0

I've tried multiple times to use the multiple .net methods for multi threading. I've encountered the same problem every time and that is trying to change UI properties from another thread.

I've read online that the BackgroundWorker has a special event that can be used to relay information back to the main thread. After trying it out, it was VERY unreliable and seemed like a cheap hack. I was very disappointed. I haven't been able to find anything about this.

Any ideas?

4

2 Answers 2

2

This isn't really specific to .NET, even. Many user interface frameworks have thread affinity.

There are many alternatives to using BackgroundWorker, including Control.BeginInvoke for Windows Forms or using the Dispatcher for WPF or Silverlight.

If you're using .NET 4, you can also use TaskScheduler.FromCurrentSynchronizationContext to execute any Task on the UI thread's synchronization context.

Sign up to request clarification or add additional context in comments.

Comments

1

The background worker's event driven is not a cheap hack.

However, if you do feel like doing the update yourself, you need to use the Control.Invoke method in order to join up with the UI thread to do updating.

You will end up littering your code with if (control.InvokeRequired) all over the place, and so I would suggest looking at using an extension method, as detailed in another post:

Cleaning up code littered with InvokeRequired

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.