Question

I write a heavily asynchronseous application.

I am looking for a way to queue method calls, similar to what BeginInvoke / EndInvoke does.... but on my OWN queue. The reaqson is that I am having my own optimized message queueing system using a threadpool but at the same time making sure every component is single threaded in the requests (i.e. one thread only handles messages for a component).

I Have a lot of messages going back and forth. For limited use, I would really love to be able to just queue a message call with parameters, instead of having to define my own parameter, method wrapping / unwrapping just for the sake of doing a lot of admnistrative calls. I also do not always want to bypass the queue, and I definitely do not want the sending service to wait for the other service to respond.

Anyone knows of a way to intercept a method call? Some way to utilize TransparentProxy / Virtual Proxy for this? ;) ServicedComponent? I would like this to be as little overhead as possible ;)

Was it helpful?

Solution

How about using lambdas?

I mean, why don't you create some Queue, and process them in manner like

while (!queue.Empty) 
{
    Action action = queue.Pop();
    action(); // this calls your action
}

You can add actions very simply:

Queue.Add( ()=>{  /* any code you wish here */})

This is just a tip, I'm not sure if there is some Queue class, but it should be pretty straightforward to create one (and threadsafe!) by yourself.

The workaround could (and should) be much wiser, but the main point is there. Write me if you want to consult.

Pz, the TaskConnect developer

OTHER TIPS

Create a queue of MethodInvoker's

Queue<MethodInvoker> EventCall = new Queue<MethodInvoker>();

Later add items to your Queue

EventCall.Enqueue(ClearAllVals);
EventCall.Enqueue(saystuff);
EventCall.Enqueue(testFunc);

Then call your functions one at a time:

MethodInvoker bb = EventCall.Dequeue();
bb();
bb = EventCall.Dequeue();
bb();
bb = EventCall.Dequeue();
bb();

to call all of your functions in a safe way (this will also remove them all from the queue leaving the queue empty and all the functions called)

public bool InvokeAll(){
    MethodInvoker bb = null; // this will hold the function prior to use
    for(int i = 0; i<EventCall.count; i++){

        bb = EventCall.Dequeue(); //pull a method off of the queue
        bb(); //call the method you pulled off of the queue

    }
}

to call them all just use InvokeAll(); or to call them one a time whenever you want:

public bool NextEvent(){
    MethodInvoker bb = null; // this will hold the function prior to use
    if(EventCall.count > 0){

        bb = EventCall.Dequeue(); //pull a method off of the queue
        bb(); //call the method you pulled off of the queue

        } else {
        MessageBox.Show("there was no event to call"); // this is optional, point being you should be handeling the fact that there is no events left in some way.
        }
}

The DynamicProxy that is part of the Castle project allows object member interception without some of the typical Marshalling pain

http://www.castleproject.org/projects/dynamicproxy/

You could use this to intercept your methods calls and then do with them what you want.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top