Determine underlying Object in Asynchronous EndInvoke().

P

Peter

Hi

I call some "worker"-Methods asyncronous. here some simplified code

....

public delegate long AsyncWCaller(string parameter);

....

// Setting the Status-Delegate, worker object is given by parameter
Logger log = new Logger();
worker.StatusEventHandler += new Worker.StatusEvent( log.LogStatus);

// later on - in workerFinishd i will do
// worker.StatusEventHandler -= new
Worker.StatusEvent( log.LogStatus);


// Starting the Job async.
AsyncWCaller asyncW = new
AsyncWCaller(worker.DoSomethingWithNotification);

IAsyncResult wAsyncResult =
asyncW.BeginInvoke( "test", new AsyncCallback(WFinished),
asyncW);

-- ok
----

private void WFinished( IAsyncResult ar )
{

AsyncWCaller caller = (AsyncWCaller) ar.AsyncState;
long result = caller.EndInvoke(ar);

// !!!! My Problem is here
// 1. I will do some logging --> result
// 2.I will remove the status event
// worker.StatusEventHandler -= new
Worker.StatusEvent( log.LogStatus);
//
// so how can i derermine the worker-Object here


}

Thanks Peter
 
A

Alberto Poblacion

Peter said:
[...]
AsyncWCaller asyncW = new
AsyncWCaller(worker.DoSomethingWithNotification);

IAsyncResult wAsyncResult =
asyncW.BeginInvoke( "test", new AsyncCallback(WFinished),
asyncW);
[...]
private void WFinished( IAsyncResult ar )
{
AsyncWCaller caller = (AsyncWCaller) ar.AsyncState;
long result = caller.EndInvoke(ar);
[...]
// so how can i derermine the worker-Object here

Basically, your problem is that you need to pass TWO things to your
callback routine: your "caller" delegate so that you can do
caller.EndInvoke, and also your "worker" object.
Since the AsyncState parameter is an object, you can pass there anything
that you want. One thing you can do is create a class to encapsulate both
things that you want to pass. Or you can use an object array, which is
itself an object:

IAsyncResult wAsyncResult =
asyncW.BeginInvoke( "test", new AsyncCallback(WFinished),
new object[]{asyncW, worker});
....
private void WFinished( IAsyncResult ar )
{
object[] state = (object[]) ar.AsyncState;
AsyncWCaller caller = (AsyncWCaller)state[0];
MyWorkerClass worker = (MyWorkerClass)state[1];
long result = caller.EndInvoke(ar);
...//use worker here
 
P

Peter

[...]
AsyncWCaller asyncW = new
AsyncWCaller(worker.DoSomethingWithNotification);
IAsyncResult wAsyncResult =
   asyncW.BeginInvoke( "test", new AsyncCallback(WFinished),
asyncW);
[...]
private void WFinished( IAsyncResult ar )
{
  AsyncWCaller caller = (AsyncWCaller) ar.AsyncState;
  long result = caller.EndInvoke(ar);
[...]
 //   so how can i derermine the worker-Object here

    Basically, your problem is that you need to pass TWO things to your
callback routine: your "caller" delegate so that you can do
caller.EndInvoke, and also your "worker" object.
    Since the AsyncState parameter is an object, you can pass there anything
that you want. One thing you can do is create a class to encapsulate both
things that you want to pass. Or you can use an object array, which is
itself an object:

IAsyncResult wAsyncResult =
    asyncW.BeginInvoke( "test", new AsyncCallback(WFinished),
    new object[]{asyncW, worker});
...
private void WFinished( IAsyncResult ar )
{
   object[] state = (object[]) ar.AsyncState;
   AsyncWCaller caller = (AsyncWCaller)state[0];
   MyWorkerClass worker = (MyWorkerClass)state[1];
   long result = caller.EndInvoke(ar);
   ...//use worker here


Thank you....

not to see the wood for the trees (manchmal sieht man den Wald vor
lauter Bäumen nicht)


Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top