I have C# windows form application that queries data from a connected device. I currently start a System.Timer.Timer that is set to fire every 20ms and grab data from the connected tool. This works fine but of course the UI becomes unresponsive for updates of data counts, end-result of calcs on the data etc etc. I also display the data in a couple OpenGL windows so there is quite a bit going on.
_timer1 = new System.Timers.Timer{
Interval = Convert.ToDouble(txtElapse.Text),
Enabled = true};
_timer1.Elapsed += Timer1Elapsed;
A good rule of thumb is that anything that takes longer or could take longer than 50ms should be asynchronous. Your options for multi-threading depend on the target framework you are using, because in .NET 2-3.5 you have the standard asynchronous patterns (begin, end, invoke, callbacks), but in .NET 4 you have the Task Parallel Library.
The BackgroundWorker is a wrapper for a asynchronous operation. So it would fit the bill for your operation. Considering you said you've done a fair amount of research on it, I won't go in to details there.
You can also spin off a thread and do thread-safe invokes to the UI.
public void StartFooAsync() {
Thread thread = new Thread(new ThreadStart(DoFooAsync));
thread.IsBackground = true;
thread.Start();
}
private void DoFooAsync() {
while (m_Running) {
// Do something
Thread.Sleep(20ms); // Make the thread wait your 20ms
}
}
You can also use the Task Parallel Library in .NET4.