Thoroughly confused about Invoke and Threads

  • Thread starter Thread starter Alex Davidson
  • Start date Start date
A

Alex Davidson

After several hours of battling to understand how to use Invoke from a
background thread to pass a result to a Forms control, I though I was doing
well until I came across the Chat example (Use Sockets - Advanced .NET
Framework (Networking)) in the Visual Basic .Net 2003 Resource Kit.

Not an Invoke in sight, and a Listbox seemingly called from the non-UI
thread. Have I missed something?
 
I just posted low level details on the Invoke paradigm for Windows Forms. I
don't think
it will necessarily help you understand your scenario, but who knows, maybe it
will. Note
that I didn't actually write any code as part of the posting.

http://weblogs.asp.net/justin_rogers/archive/2004/05/04/125625.aspx

If InvokeRequired, then Invoke is going to post a windows message to the UI
thread for your
control. Once that message is processed on the UI thread, your callback will be
called. From
within the callback you work with the data as if you were on the same thread.
Your callback
can have any form you'd like and take as many arguments as you'd like, so
passing data usually
isn't an issue. As this work is happening on the UI thread, the thread your
Invoke call was made
on is in a wait state, until the delegate returns and the synchronization
primitive is signalled to let
your thread know the operation has completed.

If you use BeginInvoke the wait operation isn't part of the process and your
code simply continues
to run immediately following the BeginInvoke and at some later time in the
future your callback will
be called.
 
Alex Davidson said:
After several hours of battling to understand how to use Invoke from a
background thread to pass a result to a Forms control, I though I was doing
well until I came across the Chat example (Use Sockets - Advanced .NET
Framework (Networking)) in the Visual Basic .Net 2003 Resource Kit.

Not an Invoke in sight, and a Listbox seemingly called from the non-UI
thread. Have I missed something?

What happens if you just call Invoke after InvokeRequired returns true? Does
it work?
 
Thanks guys for the replies. The thing that was confusing me was trying to
understand why the Chat sample didn't use Invoke, or even test for it, when
throughout the documentation it stresses the need to do so whenever you
update a control from the non-UI thread.

When I added a couple of lines to the Chat sample to test InvokeRequired, it
returned true. Seems that they've overlooked their own rules in the
sample...
 
Alex, the rule is not hard and fast. There are times when you can operate
on a WinForms control from another thread. The issue is that problems
CAN come from this, that when those problems do show up they are hard
to isolate, and they generally result in random failures in your application.
The Chat sample gets lucky, but it is playing a game of russian roulette.
 
Yes, if InvokeRequired returns true, then Invoke still works. They special
case the instance where you are already on the same thread, so there is no
harm, except for a small perf penalty, for using Invoke all the time.
 
Back
Top