Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to ensure thread is still ok? (Alive Event?) .Net 2.0 C#

Status
Not open for further replies.

Insider1984

Technical User
Feb 15, 2002
132
0
0
US
Hi there. I'm building an application which spawns a number of threads to do a lot of different simple unit style tasks. While everything seems to be nearly exception free there is a case maybe every couple days of runtime that something throws an exception. The problem is that the architecture is so "flexible" and isolated that the threads pumping data into the queue's doesn't know the working thread died.

These obviously leads to a lot of issues which is why I need a way for the application which "supports" these working threads to be aware of anything bad that happens inside of them.

My thought was to have some sort of "Keep Alive" signal triggered within the thread every "X" number of seconds to show it's still alive at a base thread level. The main application would then monitor all the threads to ensure that it received a "signal" from all tools.

I'm pretty weak in events which is where I need the help.

I understand that I'll have a AutoResetEvent in each "workerthread" and that my main application will subscribe to all events and perform a "WaitAll" on an Array of the events. Finally the private autoreset event would need to be triggered by the workthread every X seconds which may involve it's own timer based event.

What I'm having trouble wrapping my head around is how I can correctly trigger the AutoResetEvent every X seconds and ensure that I'm doing this on a thread which itself has WaitHandles on it's incoming data queue's.

What I know doesn't work is triggering the event from another thread because the event will still trigger even if the worker thread has died. The kink in this whole ordeal is that I need knowledge of a thread's death quicker than it's possibility of getting data to process. (Which is anywhere from <1ms to >1 minute.

=====================
Insider
4 year 'on the fly' programmer
C, C++, C#, MFC, Basic, Java ASP.NET
 
Okay maybe I jumped the gun on the topic. I got the event working great.... Unfortunately when the worker thread dies, the timer (created on the worker thread) doesn't stop.

There are cases where there is a low level exception occuring that even a generic "Exception" doesn't catch (seems to be coming from mscoreworks.... no more info than that at this time unfortunately....)....So I can't "stop" the timer inside the "Catch"....

=====================
Insider
4 year 'on the fly' programmer
C, C++, C#, MFC, Basic, Java ASP.NET
 
Perhaps you could store the DateTime.Now when the worker thread is started and just report the difference between DateTime.Now and the stored DateTime in your event as a way to track the processing time. This way you wouldn't need any kind of a timer object.

---------------------------------------
A picture is worth 2,000 bytes.
---------------------------------------
 
Did you try checking Thread.IsAlive?

I would write my message pump like this:

Code:
Thread workerThread = StartNewThread();
while (true)
{
    data = GetData();
    if (!workerThread.IsAlive())
    {
        workerThread = StartNewThread();
    }
    PostDataToThread(data, t);    
}

This still won't help though if your worker thread gets deadlocked or stuck in an infinite loop.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top