Does callback run on worker thread.

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

Peter Duniho

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
 
W

Willy Denoyette [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.
 
W

William Stacey [MVP]

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

archana

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

William Stacey [C# MVP]

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

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