473,465 Members | 1,651 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Async main-thread method execution

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
Feb 15 '07 #1
12 3511
Use ThreadPool to execute whatever you need, like
System.Threading.ThreadPool.QueueUserWorkItem(dele gate { OnEvent(X); });


"Tomaz Koritnik" <no****@nospam.comha scritto nel messaggio
news:aG******************@news.siol.net...
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

Feb 15 '07 #2
"Tomaz Koritnik" <no****@nospam.comwrote in message
news:aG******************@news.siol.net...
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.

Feb 15 '07 #3
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]" <wi*************@telenet.bewrote in message
news:OW*************@TK2MSFTNGP04.phx.gbl...
"Tomaz Koritnik" <no****@nospam.comwrote in message
news:aG******************@news.siol.net...
>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.

Feb 15 '07 #4
"Tomaz Koritnik" <no****@nospam.comwrote in message
news:Wo******************@news.siol.net...
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.

Feb 15 '07 #5
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.

Feb 15 '07 #6
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
Feb 15 '07 #7
In .Net 2.0, take a look at BackgroundWorkerProcess.

Known in the help as "BackgroundWorker Control".

Feb 15 '07 #8
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" <no********@gmail.comwrote in message
news:11**********************@j27g2000cwj.googlegr oups.com...
>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.

Feb 16 '07 #9
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]" <wi*************@telenet.bewrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
"Tomaz Koritnik" <no****@nospam.comwrote in message
news:Wo******************@news.siol.net...
>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.

Feb 16 '07 #10
Then why specifically do you want to push back to the main thread? One
option may be to roll a simple delegate queue on the main thread (i.e.
so that it is pumping)? But that suggests that this main loop isn't
doing anything else useful other than being a workhorse... is that
what you desire? (if so not a problem, but it seems unusual to
deliberately serialize the workers by pushing back to a single
thread).

Marc
Feb 16 '07 #11
Tomaz Koritnik <no****@nospam.comwrote:
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.
If you're going to ask others to read your posts again, you should
really re-read the replies as well. Willy suggested using BeginInvoke
instead of Invoke, which is *exactly* what you're after to make it
asynchronous (which not_a_commie also pointed out in the post you were
replying to).

As for not having a control available at the time - you'd need to make
some reference available as ISynchronizeInvoke, and you would call
BeginInvoke on that. The UI would typically pass a control as the
implementation of ISynchronizeInvoke, so that the correct thread would
be used.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 16 '07 #12
"Tomaz Koritnik" <no****@nospam.comwrote in message
news:__******************@news.siol.net...
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.
Should have said that from the beginning, anyway, I don't see why you need to execute code
on a particular thread, unless you need to access thread affinitized objects . If that's the
case, you'll have to pass a delegate to your "main thread", for instance in a queue, your
main thread will have to poll this queue or wait for a signal from the worker to pick-up the
delegate and execute it's method.

Willy.
Feb 16 '07 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Passynkov, Vadim | last post by:
I am using Asynchronous Query Processing interface from libpq library. And I got some strange results on Solaris My test select query is 'SELECT * from pg_user;' and I use select system...
0
by: DotNetShadow | last post by:
Hi Guys I came across this article which deals with Performance Considerations for Making Web Service Calls from ASPX Pages:...
2
by: Mark Harrison | last post by:
Here is a test program which dies in the postgres runtime. I've simplified the code as much as I can, and I cannot see where I'm doing anything wrong. Has anybody had success with async mode? ...
2
by: Leneise44 | last post by:
Does the new async features within asp.net 2.0 and ado.net 2.0 render the async application block (1.1) extinct? What would be the advantages of using the async application block with 2.0? Seems...
5
by: Linan | last post by:
Hi, In javascript, code could be written like this: .... var _p=XMLHttpRequest(); _p.open('GET',url,true); _p.send(null); _p.onreadystateChange=function(){
15
by: dennis.richardson | last post by:
Greetings all. Here's a problem that's been driving me nuts for the last 48 hours. I'm hoping that someone has come across this before. I have a C# Application that reads a UDP broadcast...
4
by: Marcolino | last post by:
Hi All, I'm using this code provided by Michael to run Async process: ...
3
by: Ryan Liu | last post by:
Hi, Is Async I/O (e.g. NetworkStream.Begin/End Read/Write) always better than synchronous I/O? At least as good? When I don't concern about easy or difficult to write code, should I always...
4
by: =?Utf-8?B?WWFua2VlIEltcGVyaWFsaXN0IERvZw==?= | last post by:
I need to do a simple asych post back to validate that an id is unique. I do not want to post back the entire page for this, but i want to make this part of the clientside validators. 1. i...
1
by: =?Utf-8?B?TWFyaw==?= | last post by:
Hi... There are a few questions wrapped up in this, but the main one is that the WebService.MyMethodAsync() methods that are automatically generated in the client code by VS 2005 don't seem to...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.