4

I want to monitor one thread from another thread.currently looking at threasd.isalive property. If there is any exception in thread still thread.isalive property is true.

I want to kill thread if there is any exception in thread or if the thread is in infinite loop..

Would appreacite for your inputs/solutions/suggestions.

Raju

user209293
  • 889
  • 3
  • 18
  • 27

2 Answers2

3

It sounds like the monitored thread is catching the exception it's throwing, otherwise, it would terminate and probably take your entire process down as well. You can subscribe to the AppDomain.FirstChanceException event to figure out when an exception is initially thrown, but even if that happens, you don't necessarily want to kill the thread (what if the thread catches the exception, handles it, and proceeds normally?). Instead, consider letting the exception terminate the thread "normally", and then catch it in your monitor code to prevent it from taking down the process.

There's no way to tell if a thread is in an infinite loop, but you can kill a thread that's been running for too long (see example code below). Terminating a thread forcefully with Thread.Abort, however, can cause issues and is a code smell (see here). You should consider changing the worker thread to manage its own lifetime.

class Program
{
    static void Main(string[] args)
    {
        if (RunWithTimeout(LongRunningOperation, TimeSpan.FromMilliseconds(3000)))
        {
            Console.WriteLine("Worker thread finished.");
        }
        else
        {
            Console.WriteLine("Worker thread was aborted.");
        }
    }

    static bool RunWithTimeout(ThreadStart threadStart, TimeSpan timeout)
    {
        Thread workerThread = new Thread(threadStart);

        workerThread.Start();

        bool finished = workerThread.Join(timeout);
        if (!finished)
            workerThread.Abort();

        return finished;
    }

    static void LongRunningOperation()
    {
        Thread.Sleep(5000);
    }
}
Chris Schmich
  • 29,128
  • 5
  • 77
  • 94
3

Usually this is a sign of overdesign. The vast majority of programs simply do not need this.

If you really need to detect infinite loops in threads, then you should structure the thread logic to be in one main loop and have it "checkin" to a watchdog. The main loop is of course infinite (unless the thread is expected to "finish"), but that will catch any infinite loops nested in the main loop.

The problem is: what is this "watchdog"?

It cannot be another thread in the same AppDomain. It is completely improper to Thread.Abort a thread in this scenario.

It could possibly be a thread in a separate AppDomain. Personally, I'm not completely happy with the "best-effort but not guaranteed" kind of cleanup that happens during AppDomain recycling, so I just avoid multiple AppDomains altogether.

It could be a process. This solution is fairly common in very important automation solutions.

It could also be another computer. This solution is used in critical automation solutions. Much of the work that I used to do involved watchdog processes on each computer combined with watchdog computers providing a sort of hot-backup failover cluster.

I can honestly say, though, that 98% of the time this question is asked, the answer is "just make sure you don't put an infinite loop in the thread in the first place." In other words, the huge cost of designing and implementing a watchdog solution is simply not within the scope of the real requirements. Design and code reviews combined with a good testing strategy is usually the best solution.

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810