multithreading - Manually Timing out a C# Thread -


I need to add time end to a long running thread. We have some external problems, which can sometimes hang that thread indefinitely on a certain line of code, to strengthen our process, we want to find out that the thread is now actively running / Is not voting and abrogating the thread. With this we need to clean up the resources and restart the thread.

What would be the preferred way of adding this functionality?

You need two things:

  1. Some other threads Monitoring and abbertext (your "monitor" thread)
  2. To see if the suspicious thread is still working

In your simpler version Suspicious thread updates a shared static variable that is present with a reliable frequency with the current time. How it fits into the control flow of your thread depends on you (this is the hard part - you will usually do this, important, another thread) then just wake up a second thread and check it every time if this is recent If there is no time, then cancel the thread.

  // Suspect thread foreach (whatever thing is) {// Some stuff some class. StaticVariable = DateTime.Now; } // While the monitor thread (must be stilborking) {Thread. Sleep (timespanferminates (10)); If (datetime.now.Subtract (TimespainFerminates (15) & lt; SomeClass.StaticVariable) suspectThread.Abort ()}  

Comments

Popular posts from this blog

sql - dynamically varied number of conditions in the 'where' statement using LINQ -

asp.net mvc - Dynamically Generated Ajax.BeginForm -

Debug on symbian -