0

I am attempting to make a program which updates the GUI with new data every 30 seconds. I currently have it so you can click a button to update the data values, but I want the finished product to automatically update. I am sure that it is a simple addition, perhaps with the addition of the backgroundWorker class? Or have something generate an event, which would be handled the same way the buttonClicked action listener is working. What would be the best way to go about this?

tshepang
  • 12,111
  • 21
  • 91
  • 136
Tim
  • 1,334
  • 2
  • 15
  • 29

5 Answers5

1

I would add a timer to the form and have that call the same code as the button event. You will have threading issues as the timer is on another thread. Make sure you call invoke and check out the question.

Community
  • 1
  • 1
yamspog
  • 18,173
  • 17
  • 63
  • 95
  • This is what I eventually went with. Once I get more free time I will become much more familiar with threading. Thanks! – Tim Apr 05 '11 at 22:53
1

If this is a Windows Forms application, then the easiest way to achieve this would be to use Timer component. You can specify execution interval and even handler when the timer 'ticks'.

If you are going to update the GUI from timer event handler you may need to use Application.DoEvents to make the window 'repaint'. Otherwise you may not see any changes in GUI until after all events are handled. Please read here: http://msdn.microsoft.com/en-us/library/system.windows.forms.application.doevents.aspx


Solution provided above is the easiest way. When you are familiar with the concept you may want to expand your knowledge about threads and updating GUI from threads that did not create GUI, etc. This may be interesting for you: how to update a windows form GUI from another class?

Community
  • 1
  • 1
mike
  • 53
  • 1
  • 5
0

You can use a timer:

System.Forms.Timer

Yochai Timmer
  • 48,127
  • 24
  • 147
  • 185
  • Yeah, I just need it to update for infinity time. I know while(true) should do the job, but I thought it was considered bad coding practice? – Tim Apr 05 '11 at 20:06
  • The timer will keep running and trigger in the interval you set it to. Until you stop it. – Yochai Timmer Apr 05 '11 at 20:08
0

You have to consider how long will takes fetching the data from the real DataSource. This because if you use a Winform Timer, during the timer event processing the UI will not respond to other user events, so if this is a long process the user experience became poor. Using a background worker thread would be a good strategy, but in this case you have to update the UI via Control.Invoke, or by using the PreogressChanged event. In my opinion, the second option (BackgroundWorker) is better.

Felice Pollano
  • 32,832
  • 9
  • 75
  • 115
0

I would grab a SynchronizationContext during form creation, and save it as a private field. Then in the timer event use said filed's methods to invoke the UI updating code in the context of the UI thread.

This method works for both Winforms and WPF.

You don't need to use a system.Forms.Timer, you can use this to move a code block's execution into the context of the UI thread be it from a System.Timer, ThreadPool, whatever. This Code Project article explains a bit more.

Pete Stensønes
  • 5,595
  • 2
  • 39
  • 62