Queueing method calls - any idea how?

12,557

Solution 1

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

Solution 2

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.
        }
}

Solution 3

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.

Share:
12,557
TomTom
Author by

TomTom

Updated on June 25, 2022

Comments

  • TomTom
    TomTom about 2 years

    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 ;)

  • TomTom
    TomTom about 14 years
    TGhe queueing is not the problem ;) I have that one already, and a lot of messages that transport data updates. My problem are function calls only. The queue then uses a ThreadPool to get a worker thread that processes it's content ;) I have a lot of cross process data update stuff there - now I look for a way to queue (in process) method calls. One message type that has no "data" but a function call with all the parameters, so I can basicall call methods on the target service. This would significantly cut down on the number of different messages I need...
  • TomTom
    TomTom about 14 years
    and I would not have to come up with a hugh switch statement just for calling different methods.
  • Justin Ethier
    Justin Ethier about 14 years
    Since you are looking to queue in-process method calls, passing lambdas seems to be exactly what you are looking for...