AsyncCallback / IAsyncResult

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi

At the moment, I am using an AsyncCallback to notify that a worker thread completes. The callback is called notificationCallback (a member of the thread). At the end of the work, I do

notificationCallback( asyncResult )

I was wondering, on which thread will the underlying method be called

Thanks
Tom.
 
Tom,

If I understand you correctly, notificationCallback is the method that
is being called back when the work is complete. If this is the case, then
the method will be called on the thread that was doing the work, not on the
thread that the thread was started from.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

TT (Tom Tempelaere) said:
Hi,

At the moment, I am using an AsyncCallback to notify that a worker thread
completes. The callback is called notificationCallback (a member of the
thread). At the end of the work, I do:
 
Tom
If I understand you correctly, notificationCallback is the method tha
is being called back when the work is complete. If this is the case, the
the method will be called on the thread that was doing the work, not on th
thread that the thread was started from
Hope this helps
- Nicholas Paldino [.NET/C# MVP
- (e-mail address removed)

I think you do. I'll describe it in short

Thread A is the thread that starts the worker thread (B). Thread A passes the AsyncCallback delegate-object to thread B on startup. The idea is that when thread B has done working and is about to stop, it calls the delegate to notify (1) completion and (2) results (if any) to thread A
So thread B, at the end of the operation, calls

notificationCallback( asyncResults )

My question, in concreto, is whether the actual call to the underlying method in the delegate is called on Thread A or on Thread B. If I understand you correctly, you say that the actual method is executed on thread B. Correct

To clarify. In my current setting, the workerthread is modeled as a workerthread class. Any specific worker thread derives from the base thread-class (to avoid having to rewrite plumbing code for each specific worker thread). The worker thread class takes an AsyncCallback object on construction. This is used to signal completion. Some pseudo-code

class ExampleWorkerThreadBas

ExampleWorkerThreadBase(AsyncCallback notificationCallback)
private void ThreadFn() { // actual thread functio
// ..
DoWork()
notificationCallback( asyncResults )

protected abstract void DoWork()
// ..


Now the reason why I asked the question, is that the notificationCallback could be to a UI thread which uses the results to update the UI. Now of course, the worker thread cannot touch the UI directly (at least, in MFC this was true). So the noficationCallback shouldn't either

Currently, in the UI thread I am passing a delegate to a function that calls Invoke, to ensure that the UI update is done in the UI thread

Thanks, Nicholas
Tom.
 
Tom,

Yes, the callback occurs on thread B.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

TT (Tom Tempelaere) said:
Tom,
If I understand you correctly, notificationCallback is the method
that
is being called back when the work is complete. If this is the case, then
the method will be called on the thread that was doing the work, not on the
thread that the thread was started from.
Hope this helps.
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I think you do. I'll describe it in short.

Thread A is the thread that starts the worker thread (B). Thread A passes
the AsyncCallback delegate-object to thread B on startup. The idea is that
when thread B has done working and is about to stop, it calls the delegate
to notify (1) completion and (2) results (if any) to thread A.
So thread B, at the end of the operation, calls:

notificationCallback( asyncResults );

My question, in concreto, is whether the actual call to the underlying
method in the delegate is called on Thread A or on Thread B. If I understand
you correctly, you say that the actual method is executed on thread B.
Correct?
To clarify. In my current setting, the workerthread is modeled as a
workerthread class. Any specific worker thread derives from the base
thread-class (to avoid having to rewrite plumbing code for each specific
worker thread). The worker thread class takes an AsyncCallback object on
construction. This is used to signal completion. Some pseudo-code:
class ExampleWorkerThreadBase
{
ExampleWorkerThreadBase(AsyncCallback notificationCallback);
private void ThreadFn() { // actual thread function
// ...
DoWork();
notificationCallback( asyncResults );
}
protected abstract void DoWork();
// ...
}

Now the reason why I asked the question, is that the notificationCallback
could be to a UI thread which uses the results to update the UI. Now of
course, the worker thread cannot touch the UI directly (at least, in MFC
this was true). So the noficationCallback shouldn't either.
Currently, in the UI thread I am passing a delegate to a function that
calls Invoke, to ensure that the UI update is done in the UI thread.
 
Nicholas,

Do I really need an AsyncCallback in this setting? Would it not be less
efficient to use AsyncCallback instead of some regular delegate (ie
synchronous).
Are there any other ways to signal thread termination?

Thanks,
---
Cheers,
Tom Tempelaere


Nicholas Paldino said:
Tom,

Yes, the callback occurs on thread B.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Tom,
If I understand you correctly, notificationCallback is the method that
is being called back when the work is complete. If this is the case, then
the method will be called on the thread that was doing the work, not on the
thread that the thread was started from.
Hope this helps.
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I think you do. I'll describe it in short.

Thread A is the thread that starts the worker thread (B). Thread A
passes
the AsyncCallback delegate-object to thread B on startup. The idea is that
when thread B has done working and is about to stop, it calls the delegate
to notify (1) completion and (2) results (if any) to thread A.
So thread B, at the end of the operation, calls:

notificationCallback( asyncResults );

My question, in concreto, is whether the actual call to the underlying
method in the delegate is called on Thread A or on Thread B. If I understand
you correctly, you say that the actual method is executed on thread B.
Correct?
To clarify. In my current setting, the workerthread is modeled as a
workerthread class. Any specific worker thread derives from the base
thread-class (to avoid having to rewrite plumbing code for each specific
worker thread). The worker thread class takes an AsyncCallback object on
construction. This is used to signal completion. Some pseudo-code:
class ExampleWorkerThreadBase
{
ExampleWorkerThreadBase(AsyncCallback notificationCallback);
private void ThreadFn() { // actual thread function
// ...
DoWork();
notificationCallback( asyncResults );
}
protected abstract void DoWork();
// ...
}

Now the reason why I asked the question, is that the
notificationCallback
could be to a UI thread which uses the results to update the UI. Now of
course, the worker thread cannot touch the UI directly (at least, in MFC
this was true). So the noficationCallback shouldn't either.
Currently, in the UI thread I am passing a delegate to a function that
calls Invoke, to ensure that the UI update is done in the UI thread.
Thanks, Nicholas.
Tom.
 
Back
Top