continuously running thread?

  • Thread starter Richard Blewett [DevelopMentor]
  • Start date
R

Richard Blewett [DevelopMentor]

Don't create your own thread, use the CLR threadpool via the System.Threading.Timer class. This will fire a method at a timed interval on a threadpool thread and this method can check the XML file.

However, be careful in that you sound as if you then want to update the user interface when the XML file has changed. You will need to call Control.BeginInvoke on one of the controls on the form (the datagrid itself would work well) and pass it a method you want executed on the UI thread that will perform the datagrid refresh. This is because only the UI thread is allowed to update the UI in any way.

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

Hello:
I have 2 windows application 1st one is periodically writing specific data
to XML file , the 2nd application has a dialog with DataGrid , the datagrid
filled with information in the XML generated by 1st application and should
be updated as soon as the XML file is update, so I decide to code a
continously running thread that checks the XML every specific time and
refresh the contents of datagrid
the question :
1. How can I code a thread that continously checks the XML after specific
time.

Best Regards
 
R

Raed Sawalha

Hello:
I have 2 windows application 1st one is periodically writing specific data
to XML file , the 2nd application has a dialog with DataGrid , the datagrid
filled with information in the XML generated by 1st application and should
be updated as soon as the XML file is update, so I decide to code a
continously running thread that checks the XML every specific time and
refresh the contents of datagrid
the question :
1. How can I code a thread that continously checks the XML after specific
time.

Best Regards
 
J

Justin Rogers

Be aware that the timer classes may end up consuming more than one thread... If
your working process takes a sufficiently
long amount of time, longer than the check interval, then you'll wind up with
multiple threads queued to read from the same
file. These types of concurrency issues are easier to avoid with your own
thread, and continuously running threads aren't
a negative thing or difficult to create. In addition with multiple threads that
can be doing the same work, some concurrency
constructs have to be put into place to avoid them walking on the same data
structure (if you are using shared data structures).

[pseudo-C#]
Thread thread = new Thread(new ThreadStart(Thread_ContinuousChecker));
thread.IsBackground = true;
thread.Name = "Data Polling Thread"; // <-- Useful for debugging, way more than
a worker pool thread.
thread.Start();

private void Thread_ContinuousChecker() {
while(true) {
// Do your checking
if ( newData ) { Process(); BeginInvoke(UpdateGUI); }
Thread.Sleep(/* milliseconds to wait */ 5000); // Check every 5 seconds.
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top