Invoking to a busy UI thread

G

gemartin

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

Jon Skeet [C# MVP]

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

Peter Duniho

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?

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
 

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