user34537
user34537

Reputation:

FIFO with winform in C#

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 -> Thread exception on SelectedNode = .Nodes[0]

Upvotes: 1

Views: 1536

Answers (3)

Marc Gravell
Marc Gravell

Reputation: 1063005

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.

Upvotes: 0

Richard
Richard

Reputation: 109045

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

Upvotes: 1

Anton Gogolev
Anton Gogolev

Reputation: 115779

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

Upvotes: 2

Related Questions