tags:

views:

555

answers:

3

I'm getting annoyed with GUI problems in my threads. How do i create a FIFO so my main form/thread will receive data to do things instead of having my threads use a callback and run the code (and fail) themselves?

The gui problem in question -> http://stackoverflow.com/questions/729359/thread-exception-on-selectednode-nodes0/729377

+2  A: 

Use SynchronizationContext to Post/Send "calls" to the UI thread.

Anton Gogolev
A: 

BeginInvoke on a Winform control, to make a call across threads, will use the Window's message queue, which is FIFO.

Richard
Actually, I've seen examples where an item is able to accidentally jump and effectively block the queue; I can't remember the "how", but it wasn't complex.
Marc Gravell
You can put a message at the top of the queue (can't remember which API it is), but to block message processing this would require the message processing itself to block, not putting messages on the queue.
Richard
A: 

If you have a handle to the main form (or any control), you can use Control.Invoke / Control.BeginInvoke.

If you don't want to hand out a Control instance, you can give external code an ISynchronizeInvoke instance (any control will do it, or write your own class that wraps a Control to prevent caller-casting). Then the caller can use this to perform methods.

Finally, consider using events; the running code raises events that your UI handles; the UI can then call Control.Invoke locally to process the data.

Marc Gravell
Thanks, i'll try ISynchronizeInvoke. The problem i have ATM is with control.invoke http://stackoverflow.com/questions/729359/thread-exception-on-selectednode-nodes0/729377#729377
acidzombie24
I doubt this is related; but you've ignored the plea for more info on the other post...
Marc Gravell