how to stop executing code in thread if takes to long. I have few threads which working all the time but after while code in thread is executing too long and application stop responding.
is it possible that if code is not executed in 30s thread will stop executing it and go to next code... so application will still be alive and will not stop responding. i am using C# .net 3.5
You can do this by waiting on your worker thread from a monitoring thread for a specified amount of time and then forcefully killing the worker thread if it hasn't already completed. See the example code below. In general, however, killing a thread forcefully with Thread.
A thread automatically terminates when it returns from its entry-point routine. A thread can also explicitly terminate itself or terminate any other thread in the process, using a mechanism called cancelation.
If the thread that calls Abort holds a lock that the aborted thread requires, a deadlock can occur. If Abort is called on a thread that has not been started, the thread will abort when Start is called. If Abort is called on a thread that is blocked or is sleeping, the thread is interrupted and then aborted.
In C#, a Sleep() method temporarily suspends the current execution of the thread for specified milliseconds, so that other threads can get the chance to start the execution, or may get the CPU for execution. There are two methods in the overload list of Thread. Sleep Method as follows: Sleep(Int32)
My answer here is similar to the one I posted here.
You can do this by waiting on your worker thread from a monitoring thread for a specified amount of time and then forcefully killing the worker thread if it hasn't already completed. See the example code below.
In general, however, killing a thread forcefully with Thread.Abort
is not a good idea since the target thread is not necessarily in a known state and could have open handles to resources that might not be freed. Using Thread.Abort
is a code smell.
The cleaner way is to change the worker thread to manage its own lifetime. The worker thread could check how long it has executed at well-known checkpoints and then stop if it has exceeded some limit. This approach has the drawback of requiring potentially many checkpoints scattered throughout the actual work the thread is doing. Also, the worker thread could easily exceed a limit by doing too much computation between checkpoints.
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With