Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / July 2007

Tip: Looking for answers? Try searching our database.

Invoking to a busy UI thread

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
gemartin@earthlink.net - 31 Jul 2007 23:33 GMT
Here's my question:

I have Windows Forms app.  Suppose the user clicks on the "Processing"
button on the UI.  The UI thread then starts processing.  During this
processing another thread calls invoke to update a control on the UI.

When the UI thread reaches the end of its time slice--even though it
has not completed it's processing--can the other thread invoke onto
the UI thread and do the update it wants?

My intuition (and some brief testing) tells me that the invoke will
not happen until the UI thread is completely finished with its
processing, but I haven't found it stated anywhere.  Does anyone know
the answer to this?

Thanks.
Jon Skeet [C# MVP] - 01 Aug 2007 00:02 GMT
> Here's my question:
>
[quoted text clipped - 5 lines]
> has not completed it's processing--can the other thread invoke onto
> the UI thread and do the update it wants?

No.

> My intuition (and some brief testing) tells me that the invoke will
> not happen until the UI thread is completely finished with its
> processing, but I haven't found it stated anywhere.  Does anyone know
> the answer to this?

Yes, that's the case - unless you call the horrible DoEvents method
(which introduces the spectre of re-entrancy etc).

You should avoid the UI thread doing a lot of processing though - it's
not just your other thread which might want to do things with the UI.
The user might move another window in front of it and then away again,
or resize it, or move it, or any number of other things which require
the UI thread's attention.

Signature

Jon Skeet - <skeet@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

Peter Duniho - 01 Aug 2007 00:05 GMT
> Here's my question:
>
[quoted text clipped - 10 lines]
> processing, but I haven't found it stated anywhere.  Does anyone know
> the answer to this?

Your intuition is almost correct, and while I don't know whether it's
stated explicitly somewhere (it very well may be, actually), it is a
natural consequence of what the invoke does.  In particular, using
Invoke() or BeginInvoke() queues the delegate and parameters for
execution on the target's owning thread.

There is no way for that thread to execute the code until it reaches a
point where it can retrieve the delegate and execute that, and that
can't happen as long as the thread is stuck doing whatever processing
you've got it doing.

The one exception to your intuitive sense of the problem is that it is
not actually necessary for the processing code to complete in order for
the invoked delegate to be run.  For example, you could call
Application.DoEvents(), which would process any queued invocations.
IMHO, it's a bad idea to design your code this way, but it is possible.

AFAIK, in the managed environment, DoEvents() is the only thing that
would allow invoked delegates to run.  Outside of managed code, there
are other means of doing similar things, but I don't think they are
relevant here.

Pete

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.