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?

Freesnöw
  • 30,619
  • 30
  • 89
  • 138
  • http://msdn.microsoft.com/en-us/library/system.windows.forms.control.invokerequired.aspx – asawyer Apr 26 '11 at 18:53
  • Thanks for posting exactly to what I referred to as the cheap and unreliable hack. – Freesnöw Apr 26 '11 at 18:54
  • At least everyone is on the same page then :) – asawyer Apr 26 '11 at 18:55
  • se more help here : http://stackoverflow.com/questions/181374/visual-c-form-update-results-in-flickering and http://stackoverflow.com/questions/206867/what-is-the-best-way-to-update-form-controls-from-a-worker-thread – lsalamon Apr 26 '11 at 18:57

2 Answers2

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.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
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

Community
  • 1
  • 1
John Gietzen
  • 48,783
  • 32
  • 145
  • 190