Does callback run on worker thread.

  • Thread starter Thread starter archana
  • Start date Start date
A

archana

Hi all,

I am having one confusion regarding invoking method through threading.

I have delcared one delegate. and i have one class which has parameter
of type delegate which i declared.

I have passed address of function which needs to be invoked when my
thread procedure complete.

So my question is will callback run on worker thread or on UI thread.

And if i want to update controls which i there on form in callback. Do
i need to lock that control before updating its text. Like do i need to
call invoke method on that control to provide synchronization for
updating text control so as to prevent two threads to simultanously
update same control.

Please correct me if i am wrong.

Thanks in advance.
 
archana said:
I have passed address of function which needs to be invoked when my
thread procedure complete.

When you say "invoke", do you mean you actually call Invoke?
So my question is will callback run on worker thread or on UI thread.

Assuming you are using Invoke or BeginInvoke, the callback itself will run
on the UI thread. If you use Invoke, your invoking thread will block until
the UI thread has handled the delegate. If you use BeginInvoke, a worker
thread will do that instead.
And if i want to update controls which i there on form in callback. Do
i need to lock that control before updating its text. Like do i need to
call invoke method on that control to provide synchronization for
updating text control so as to prevent two threads to simultanously
update same control.

As long as you are using Invoke or BeginInvoke, the invoked method will run
on the correct thread and you need not worry about any other
synchronization.

Pete
 
| | > I have passed address of function which needs to be invoked when my
| > thread procedure complete.
|
| When you say "invoke", do you mean you actually call Invoke?
|
| > So my question is will callback run on worker thread or on UI thread.
|
| Assuming you are using Invoke or BeginInvoke, the callback itself will run
| on the UI thread. If you use Invoke, your invoking thread will block
until
| the UI thread has handled the delegate. If you use BeginInvoke, a worker
| thread will do that instead.
|

No worker thread get's involved here, the message is posted and the caller
simply returns without blocking.

Willy.
 
And just to add... Which means the delegate is always run by the only UI
thread (hence no cross-thread operation).

--
William Stacey [MVP]

|
| || || > I have passed address of function which needs to be invoked when my
|| > thread procedure complete.
||
|| When you say "invoke", do you mean you actually call Invoke?
||
|| > So my question is will callback run on worker thread or on UI thread.
||
|| Assuming you are using Invoke or BeginInvoke, the callback itself will
run
|| on the UI thread. If you use Invoke, your invoking thread will block
| until
|| the UI thread has handled the delegate. If you use BeginInvoke, a worker
|| thread will do that instead.
||
|
| No worker thread get's involved here, the message is posted and the caller
| simply returns without blocking.
|
| Willy.
|
|
 
Hi all,

Thanks for reply.

Now my doubt is if callback is running on UI thread, if in call back i
checked currentthread.name property it is giving me name of worker
thread and not name of UI thread.

Can someone tell me what is reason behind this behaviour.

Any help will be truely appreciated.

Thanks in advance.
 
Please show some code to help us see behavior.

--
William Stacey [C# MVP]

| Hi all,
|
| Thanks for reply.
|
| Now my doubt is if callback is running on UI thread, if in call back i
| checked currentthread.name property it is giving me name of worker
| thread and not name of UI thread.
|
| Can someone tell me what is reason behind this behaviour.
|
| Any help will be truely appreciated.
|
| Thanks in advance.
|
 
Back
Top