Rob Sutherland Rob Sutherland - 1 month ago 8
C# Question

Responsive UI in windows form accessing device data

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;

I then use then invalidate the form to redraw for the OpenGL and that is where all my calcs etc are carried out.

My question is to do with threading, basically at what point or to what object do I apply the thread logic to? The timer itself? If i do that I am unable to debug, it just sits there. I have done a fair amount of research to do with ThreadPools, Backgroundworker, Thread etc but nothing for this situation as I believe (mistakenly?) that the timer runs on its own thread by default.


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;

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.