What is the best way to avaoid cross thread operations?

  • Thread starter Thread starter Pasan Indeewara
  • Start date Start date
P

Pasan Indeewara

Is there a better way to avoid cross thread operations which are making
conflicts conflicts with GUI thread.
Anyone got Some coding example to explain pls.
 
Is there a better way to avoid cross thread operations which are making
conflicts conflicts with GUI thread.

Two options:

-- Don't execute code on any thread other than the main GUI thread
-- Use Control.Invoke() to execute code on the main GUI thread

Note that the #2 option is really just a special-case of the #1 option.
Anyone got Some coding example to explain pls.

MSDN has examples, and you'll find a number in this newsgroup if you just
use Google.

Pete
 
See this:

http://msdn.microsoft.com/en-us/library/ms171728(VS.80).aspx

Note that you can begin a method with a test for InvokeRequired, and if
InvokeRequired is true, have the method call itself cross-thread via
Invoke; otherwise it just proceeds with whatever it is supposed to do.

Note that there's no point at all in checking InvokeRequired. The
Invoke() method has to do the exact same check, and the net effect will be
identical. IMHO it's better to just always Invoke() a different method
(named or anonymous) and let Invoke() deal with the threading issues. The
code is much simpler that way, and you avoid the redundant InvokeRequired
check in the common case (in fact, in the MSDN-proposed pattern,
InvokeRequired is checked _three_ times...twice by the client code, and
once by Invoke() itself).

Pete
 
Back
Top