PC Review


Reply
Thread Tools Rate Thread

Async main-thread method execution

 
 
Tomaz Koritnik
Guest
Posts: n/a
 
      15th Feb 2007
Is it possible to asynchronously call a method from worker thread so that
this method would execute in main-thread? I'm doing some work in worker
thread and would like to report progress to main thread to update some
controls. But I'd like to do that asynchronously so that worker thread would
not block until main thread finished updating and refreshing controls which
take some time. Using invoke is out since it blocks worker thread until
method finishes.

regards
Tomaz


 
Reply With Quote
 
 
 
 
Laura T.
Guest
Posts: n/a
 
      15th Feb 2007
Use ThreadPool to execute whatever you need, like
System.Threading.ThreadPool.QueueUserWorkItem(delegate { OnEvent(X); });




"Tomaz Koritnik" <(E-Mail Removed)> ha scritto nel messaggio
news:aG_Ah.422$(E-Mail Removed)...
> Is it possible to asynchronously call a method from worker thread so that
> this method would execute in main-thread? I'm doing some work in worker
> thread and would like to report progress to main thread to update some
> controls. But I'd like to do that asynchronously so that worker thread
> would not block until main thread finished updating and refreshing
> controls which take some time. Using invoke is out since it blocks worker
> thread until method finishes.
>
> regards
> Tomaz
>



 
Reply With Quote
 
Willy Denoyette [MVP]
Guest
Posts: n/a
 
      15th Feb 2007
"Tomaz Koritnik" <(E-Mail Removed)> wrote in message
news:aG_Ah.422$(E-Mail Removed)...
> Is it possible to asynchronously call a method from worker thread so that this method
> would execute in main-thread? I'm doing some work in worker thread and would like to
> report progress to main thread to update some controls. But I'd like to do that
> asynchronously so that worker thread would not block until main thread finished updating
> and refreshing controls which take some time. Using invoke is out since it blocks worker
> thread until method finishes.
>
> regards
> Tomaz
>


Use BeginInvoke instead of Invoke!

Willy.



 
Reply With Quote
 
Tomaz Koritnik
Guest
Posts: n/a
 
      15th Feb 2007
To Laura T. also...

You didn't understand my question. I'm already executing some work in a
background thread but I would like to execute a method in main-thread.
Myexample: worker thread has some data which has to be displayed. I must not
update controls from this thread, so I must somehow pass this data to
main-thread. And I wan't to do this async so worker thread continues working
while controls are updated. In Win32 the perfect solution to this is to send
message. Get my point now?

regards
Tomaz



"Willy Denoyette [MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "Tomaz Koritnik" <(E-Mail Removed)> wrote in message
> news:aG_Ah.422$(E-Mail Removed)...
>> Is it possible to asynchronously call a method from worker thread so that
>> this method would execute in main-thread? I'm doing some work in worker
>> thread and would like to report progress to main thread to update some
>> controls. But I'd like to do that asynchronously so that worker thread
>> would not block until main thread finished updating and refreshing
>> controls which take some time. Using invoke is out since it blocks worker
>> thread until method finishes.
>>
>> regards
>> Tomaz
>>

>
> Use BeginInvoke instead of Invoke!
>
> Willy.
>
>
>



 
Reply With Quote
 
Willy Denoyette [MVP]
Guest
Posts: n/a
 
      15th Feb 2007
"Tomaz Koritnik" <(E-Mail Removed)> wrote in message
news:Wo%Ah.423$(E-Mail Removed)...
> To Laura T. also...
>
> You didn't understand my question. I'm already executing some work in a background thread
> but I would like to execute a method in main-thread. Myexample: worker thread has some
> data which has to be displayed. I must not update controls from this thread, so I must
> somehow pass this data to main-thread. And I wan't to do this async so worker thread
> continues working while controls are updated. In Win32 the perfect solution to this is to
> send message. Get my point now?


Yes, I got your point and that's exactly what I meant, from your worker thread you need to
call Control.BeginInvoke and pass it a delegate who's method will execute on the UI thread.
Check the docs for more info and samples.

Willy.

 
Reply With Quote
 
not_a_commie
Guest
Posts: n/a
 
      15th Feb 2007
> You didn't understand my question. I'm already executing some work in a
> background thread but I would like to execute a method in main-thread.
> Myexample: worker thread has some data which has to be displayed. I must not
> update controls from this thread, so I must somehow pass this data to
> main-thread. And I wan't to do this async so worker thread continues working
> while controls are updated. In Win32 the perfect solution to this is to send
> message. Get my point now?


I think that they did understand. Whenever you create a control/form/
someUIpiece, the thread that is created on becomes the UI (aka, main)
thread. When you want to add children to a control, they have to be
created in the same thread as their parent. Whenever you want to call
some property or method on a control, you have to make that call from
the thread where the control was created. To make this easy, the base
class for all controls and forms has Invoke and BeginInvoke methods.
You can call those functions from any thread (and same with the
Invalidate function). You pass the invokers a (delegate) function to
be called from the control's thread. BeginInvoke is asynchronous.
Invoke is synchronous (aka, blocking).

BTW, sending messages is certainly not the best solution; it's a pain
in the ars. I'm glad that .NET and SWT support these other invoke
methodologies.

 
Reply With Quote
 
=?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
Guest
Posts: n/a
 
      15th Feb 2007
In .Net 2.0, take a look at BackgroundWorkerProcess. This provides a
simplified model for asynchrous processing with notification via the
asyncresult back to the main thread.
Peter

--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net




"Tomaz Koritnik" wrote:

> Is it possible to asynchronously call a method from worker thread so that
> this method would execute in main-thread? I'm doing some work in worker
> thread and would like to report progress to main thread to update some
> controls. But I'd like to do that asynchronously so that worker thread would
> not block until main thread finished updating and refreshing controls which
> take some time. Using invoke is out since it blocks worker thread until
> method finishes.
>
> regards
> Tomaz
>
>
>

 
Reply With Quote
 
not_a_commie
Guest
Posts: n/a
 
      15th Feb 2007
> In .Net 2.0, take a look at BackgroundWorkerProcess.

Known in the help as "BackgroundWorker Control".

 
Reply With Quote
 
Tomaz Koritnik
Guest
Posts: n/a
 
      16th Feb 2007
No, again, not my point. read my post again please. Yes It's possible to use
Invoke to execute in main thread, but this will block worker thread until
method in main thread returns. As far as I know, using invoke is synchronous
process. That's exactly what I don't want. I want async call so that worker
thread continues WHILE main thread updates controls.

regards
Tomaz


"not_a_commie" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>> You didn't understand my question. I'm already executing some work in a
>> background thread but I would like to execute a method in main-thread.
>> Myexample: worker thread has some data which has to be displayed. I must
>> not
>> update controls from this thread, so I must somehow pass this data to
>> main-thread. And I wan't to do this async so worker thread continues
>> working
>> while controls are updated. In Win32 the perfect solution to this is to
>> send
>> message. Get my point now?

>
> I think that they did understand. Whenever you create a control/form/
> someUIpiece, the thread that is created on becomes the UI (aka, main)
> thread. When you want to add children to a control, they have to be
> created in the same thread as their parent. Whenever you want to call
> some property or method on a control, you have to make that call from
> the thread where the control was created. To make this easy, the base
> class for all controls and forms has Invoke and BeginInvoke methods.
> You can call those functions from any thread (and same with the
> Invalidate function). You pass the invokers a (delegate) function to
> be called from the control's thread. BeginInvoke is asynchronous.
> Invoke is synchronous (aka, blocking).
>
> BTW, sending messages is certainly not the best solution; it's a pain
> in the ars. I'm glad that .NET and SWT support these other invoke
> methodologies.
>



 
Reply With Quote
 
Tomaz Koritnik
Guest
Posts: n/a
 
      16th Feb 2007
It's not exactly what I want. I can't use control's invoke since I don't
have any controls at the beginning. I want to call some function iside some
class that is not a control. More, it actually has nothing to do with
controls because this is an app lower layer where UI elements are not
present (and not even accessible). I may not even have an UI (service).

Therefore, again, I need to call some method in some class (that is NOT a
control) to execute in main thread.

regards
Tomaz

"Willy Denoyette [MVP]" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> "Tomaz Koritnik" <(E-Mail Removed)> wrote in message
> news:Wo%Ah.423$(E-Mail Removed)...
>> To Laura T. also...
>>
>> You didn't understand my question. I'm already executing some work in a
>> background thread but I would like to execute a method in main-thread.
>> Myexample: worker thread has some data which has to be displayed. I must
>> not update controls from this thread, so I must somehow pass this data to
>> main-thread. And I wan't to do this async so worker thread continues
>> working while controls are updated. In Win32 the perfect solution to this
>> is to send message. Get my point now?

>
> Yes, I got your point and that's exactly what I meant, from your worker
> thread you need to call Control.BeginInvoke and pass it a delegate who's
> method will execute on the UI thread. Check the docs for more info and
> samples.
>
> Willy.
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Async Execution and Thread-Safety Stefan Hoffmann Microsoft C# .NET 2 3rd Sep 2008 01:40 PM
Invoking a method from the main thread, from a worker thread. DaTurk Microsoft C# .NET 6 30th Aug 2007 08:07 PM
progress from Blocking method? or blocking main thread with async method?? giddy Microsoft C# .NET 2 24th Jun 2007 08:31 PM
Calling a main thread method from a worker thread Hao L Microsoft C# .NET 5 2nd May 2005 08:31 PM
tell in async way ,to other thread to execute method roni Microsoft Dot NET 1 11th Apr 2005 09:13 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:17 PM.