473,657 Members | 2,515 Online
Bytes | Software Development & Data Engineering Community
+ 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 3534
Use ThreadPool to execute whatever you need, like
System.Threadin g.ThreadPool.Qu eueUserWorkItem (delegate { 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.bewro te in message
news:OW******** *****@TK2MSFTNG P04.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.BeginIn voke 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 BackgroundWorke rProcess. 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 BackgroundWorke rProcess.

Known in the help as "BackgroundWork er 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_comm ie" <no********@gma il.comwrote in message
news:11******** **************@ j27g2000cwj.goo glegroups.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.bewro te in message
news:%2******** ********@TK2MSF TNGP05.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.BeginIn voke 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

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

Similar topics

0
1829
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 synchronous I/O multiplexer in 'C' The first test sends 10000 select queries using 10 nonblocking connections to database ( PQsendQuery ). The second test sends the same 10000 select queries using 1 connection ( PQexec ).
0
1313
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: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnservice/html/service07222003.asp The article talks about 3 approaches synchronous | asyncronous | and PrerequestHandler with asynchronous calls. I tried the sample that came with the article and the concept proves right.
2
2647
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? Am I doing something wrong here? Or are there some problems with async operation that I don't know about? (the larger program does this in an event loop... this non-event loop program exhibits the same behavior.) Many TIA,
2
1536
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 like a lot less code can be written using 2.0 that threads safely and quickly.
5
2788
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
8397
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 (asynchronously). Then it repackages these UDP packets and sends them to a subscriber via TCP. Now, I can read the UDP stream all day long without the application
4
2455
by: Marcolino | last post by:
Hi All, I'm using this code provided by Michael to run Async process: http://groups.google.com/group/microsoft.public.dotnet.languages.vb/browse_thread/thread/3dda80ed45e6176e/606d19fccc330588#606d19fccc330588 Now I have a new need. I have to start a Soubroutine when the Async process is finished. Many Thanks.
3
3573
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 use Async I/O?
4
2192
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 already have a custom validator that checks for a correctly formatted id and the javascript works w/o problem. Any help would be appreciated. -- Share The Knowledge. I need all the help I can get and so do you!
1
1966
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 be finishing for me. We have a VS add-in written in .net that used to make a number of database calls to fill forms. To improve security, we moved the db calls to a web service and return DataSets for the calls (the result sets are always...
0
8392
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8305
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8825
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8605
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5632
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4151
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2726
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 we have to send another system
2
1953
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.