469,630 Members | 1,254 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,630 developers. It's quick & easy.

continuously running thread?

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
Nov 16 '05 #1
2 7284
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
Nov 16 '05 #2
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.
}
}
--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

"Richard Blewett [DevelopMentor]" <ri******@develop.com> wrote in message
news:e8*************@TK2MSFTNGP11.phx.gbl...
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

Nov 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Dan | last post: by
9 posts views Thread by mareal | last post: by
2 posts views Thread by =?Utf-8?B?QWxwaGFwYWdl?= | last post: by
3 posts views Thread by bgold12 | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.