Threading Question

  • Thread starter Thread starter Tim
  • Start date Start date
T

Tim

Hi,

I have developed a windows forms application that works fine on a single
thread. Part of the requirements of this app is that it monitors the
status of a couple of services.

So I think that the best way to do this is on a separate thread. It will
check every 60 seconds or so to make sure the services are still running.

First of all is this the best way? Second assuming that it is can
someone give me a nudge in the right direction as to how to implement
the thread?

Also, if my app starts a thread and then later the app is terminated
will the second thread be terminated implicitly or do I have to
explicitly do something?

Thanks

Tim
 
Tim,

I would use a Timer instance from the System.Timers namespace. The
event for the timer will be fired on a thread other than your UI, so you
don't have to worry about creating the threads and whatnot.

All you have to do is worry about updating the UI correctly (which is
easy with a call to the Invoke method on a control created on the thread).

Hope this helps.
 
Hi Tim,
So I think that the best way to do this is on a separate thread. It will
check every 60 seconds or so to make sure the services are still running.

This is an excellent use for Threading. the thread will not interrupt the
user interface by blocking at any time.
Second assuming that it is can someone give me a nudge in the right
direction as to how to implement the thread?

Here is an excellent set of articles that will introduce you to
multi-threading in .Net apps:

http://www.yoda.arachsys.com/csharp/threads/

Creating and running a thread is easy; having it interact with the main
program thread and other threads is where it gets complicated.

In your case, it may be as simple as creating a class that does the
monitoring work, and raises events when certain conditions arise. The
Windows Forms app can subscribe to and react to the events raised in the
class running in the spawned thread. It is important to note, however, that
(1) the child thread should never attempt to manipulate the Controls on the
Form, and (2) that the Event Handler for the child thread events will need
some special attention, due to the asynchronous manner in which it is
called.

Now, depending on what version of the .Net Framework you're using, you can
handle this in several ways. The following articles were written for the 1.1
Framework, but the ideas will work on the 2.0 Framework as well:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnforms/html/winforms06112002.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnforms/html/winforms08162002.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnforms/html/winforms08162002.asp

The 2.0 Framework has some new classes that are designed for Windows Forms
as well. You may want to read up on the
System.ComponentModel.BackgroundWorker class if you're running 2.0.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but it takes a very long time,
and the bicycle has to *want* to change.
 
Hi,

He can use instead the Timer from System.Windows.Forms , in this case the
event is assured to run in the main thread.

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


Nicholas Paldino said:
Tim,

I would use a Timer instance from the System.Timers namespace. The
event for the timer will be fired on a thread other than your UI, so you
don't have to worry about creating the threads and whatnot.

All you have to do is worry about updating the UI correctly (which is
easy with a call to the Invoke method on a control created on the thread).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)
Tim said:
Hi,

I have developed a windows forms application that works fine on a single
thread. Part of the requirements of this app is that it monitors the
status of a couple of services.

So I think that the best way to do this is on a separate thread. It will
check every 60 seconds or so to make sure the services are still running.

First of all is this the best way? Second assuming that it is can someone
give me a nudge in the right direction as to how to implement the thread?

Also, if my app starts a thread and then later the app is terminated will
the second thread be terminated implicitly or do I have to explicitly do
something?

Thanks

Tim
 
Him

Kevin Spencer said:
Hi Tim,


This is an excellent use for Threading. the thread will not interrupt the
user interface by blocking at any time.

IMO that is not the case, a timer is much better, otherwise yo would have a
suspended threadfor nothing ( in between check internals)
 
True, but his app would then freeze up if the server were down (since
these things are usually determined by having requests of some sort time
out).


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Ignacio Machin ( .NET/ C# MVP ) said:
Hi,

He can use instead the Timer from System.Windows.Forms , in this case the
event is assured to run in the main thread.

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


Nicholas Paldino said:
Tim,

I would use a Timer instance from the System.Timers namespace. The
event for the timer will be fired on a thread other than your UI, so you
don't have to worry about creating the threads and whatnot.

All you have to do is worry about updating the UI correctly (which is
easy with a call to the Invoke method on a control created on the
thread).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)
Tim said:
Hi,

I have developed a windows forms application that works fine on a single
thread. Part of the requirements of this app is that it monitors the
status of a couple of services.

So I think that the best way to do this is on a separate thread. It will
check every 60 seconds or so to make sure the services are still
running.

First of all is this the best way? Second assuming that it is can
someone give me a nudge in the right direction as to how to implement
the thread?

Also, if my app starts a thread and then later the app is terminated
will the second thread be terminated implicitly or do I have to
explicitly do something?

Thanks

Tim
 
That would work fine, as long as the process running in the task is not
lengthy. Otherwise, it would block execution until it finishes.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but it takes a very long time,
and the bicycle has to *want* to change.

Ignacio Machin ( .NET/ C# MVP ) said:
Hi,

He can use instead the Timer from System.Windows.Forms , in this case the
event is assured to run in the main thread.

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


Nicholas Paldino said:
Tim,

I would use a Timer instance from the System.Timers namespace. The
event for the timer will be fired on a thread other than your UI, so you
don't have to worry about creating the threads and whatnot.

All you have to do is worry about updating the UI correctly (which is
easy with a call to the Invoke method on a control created on the
thread).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)
Tim said:
Hi,

I have developed a windows forms application that works fine on a single
thread. Part of the requirements of this app is that it monitors the
status of a couple of services.

So I think that the best way to do this is on a separate thread. It will
check every 60 seconds or so to make sure the services are still
running.

First of all is this the best way? Second assuming that it is can
someone give me a nudge in the right direction as to how to implement
the thread?

Also, if my app starts a thread and then later the app is terminated
will the second thread be terminated implicitly or do I have to
explicitly do something?

Thanks

Tim
 
Ignacio said:
Him



IMO that is not the case, a timer is much better, otherwise yo would have a
suspended threadfor nothing ( in between check internals)

Thank you all for your opinions. I did use a system timer and it is
working fine.

Thanks

Tim
 
Back
Top