GUI - Threading question

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

Tim Gallivan

Hello group,

I have a form that starts a thread via a ThreadStart. The thread lives for
the lifetime of the form, doing processing and so on. My question is: In
such a situation, what is the accepted way to monitor the thread to make
sure it hasn't crashed. Do you use a timer and check the thread's IsAlive or
it's ThreadState every few seconds/minutes, or is there another way?

Suggestions, links are most welcome!

Thanks in advance,
 
Tim,

What I would do is create another thread on top of that, and then pass
the Thread instance that represents the thread you want to check the status
of to that other thread. Then, in this thread, you would call the Join
method. This will cause the thread to block until the other thread
completes.

Then, in the other thread, you can fire an event when the Join method
returns.

Hope this helps.
 
Thanks, Nicholas. Once again, you earned your volunteer salary!

--
Tim Gallivan
I know I'm a great teacher because when I give a lesson, the person never
comes back.

Nicholas Paldino said:
Tim,

What I would do is create another thread on top of that, and then pass
the Thread instance that represents the thread you want to check the status
of to that other thread. Then, in this thread, you would call the Join
method. This will cause the thread to block until the other thread
completes.

Then, in the other thread, you can fire an event when the Join method
returns.

Hope this helps.


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

Tim Gallivan said:
Hello group,

I have a form that starts a thread via a ThreadStart. The thread lives for
the lifetime of the form, doing processing and so on. My question is: In
such a situation, what is the accepted way to monitor the thread to make
sure it hasn't crashed. Do you use a timer and check the thread's IsAlive
or
it's ThreadState every few seconds/minutes, or is there another way?

Suggestions, links are most welcome!

Thanks in advance,
 
Another way would be:
1) Have a public status var (bool, enum, other) that is a public or internal
property of the form. This property would be sync'd via a monitor lock.
2) The worker thread will be a member of its own object (i.e. WorkerObject,
etc). This object will have a back reference to the form so it can get/set
the public status property.
3) At appropriate points in the Thread method (i.e. before it blocks on
receive, after it gets data, etc.) post an update to the public field using
the reference.
4) You can have a timer method on the form that queries this shared field
(struct, class, var) for the status info and make correct action on the
form.
5) Optionally, you can also have the worker call a public Update method on
the form that invokes work on the gui thread to make updates. This would
eliminate the need for the timer, but updates can only happen when the
worker decides.
 
Hello group,

I have a form that starts a thread via a ThreadStart. The thread lives for
the lifetime of the form, doing processing and so on. My question is: In
such a situation, what is the accepted way to monitor the thread to make
sure it hasn't crashed. Do you use a timer and check the thread's IsAlive or
it's ThreadState every few seconds/minutes, or is there another way?

There are many ways to approach this problem. I prefer to not poll
things but use events/messages instead.

After you have done the ThreadStart, so the thread object exists, but
before doing the yourthread.Start() that actually causes it to start
running I would set two public properties in the thread object, one
of type Control (tellWho), and one of a delegate that takes a message
string or result code (tellWhat).

Wrap your thread's "mainloop" in a try/finally block to regain control
in case the thread terminates for any reason.

Inside of the /finally/ clause you would just have to do a
tellWho.BeginInvoke(tellWhat,new object[]{message,code,etc});

When you get this event, you know the thread is dying.

Oz
 
Sorry, I'd lost this thread. Thanks to William and oz ..., I prefer the
latter's method.

--
Tim Gallivan
I know I'm a great teacher because when I give a lesson, the person never
comes back.

ozbear said:
Hello group,

I have a form that starts a thread via a ThreadStart. The thread lives for
the lifetime of the form, doing processing and so on. My question is: In
such a situation, what is the accepted way to monitor the thread to make
sure it hasn't crashed. Do you use a timer and check the thread's IsAlive or
it's ThreadState every few seconds/minutes, or is there another way?

There are many ways to approach this problem. I prefer to not poll
things but use events/messages instead.

After you have done the ThreadStart, so the thread object exists, but
before doing the yourthread.Start() that actually causes it to start
running I would set two public properties in the thread object, one
of type Control (tellWho), and one of a delegate that takes a message
string or result code (tellWhat).

Wrap your thread's "mainloop" in a try/finally block to regain control
in case the thread terminates for any reason.

Inside of the /finally/ clause you would just have to do a
tellWho.BeginInvoke(tellWhat,new object[]{message,code,etc});

When you get this event, you know the thread is dying.

Oz
--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Back
Top