I need to abort a thread if the code takes more than 3 seconds to execute. I am using the below method.
public static void Main(string[] args) {
if (RunWithTimeout(LongRunningOperation, TimeSpan.FromMilliseconds(3000))) {
Console.WriteLine("Worker thread finished.");
} else {
Console.WriteLine("Worker thread was aborted.");
}
}
public 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;
}
public static void LongRunningOperation() {
Thread.Sleep(5000);
}
Can you please tell how can I do the same thing for the function having parameters. For example:
public static Double LongRunningOperation(int a,int b) {
}