My requiremet is to abort the thread when the code takes more than 3 seconds to excecute. I am following 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);
}
Could ypu pleas tell me how can i do the same for the code having parameters? like below?
Public static Double LongRunningOperation(int a,int b)
{
}