如果函数在5分钟内没有完成,如何终止函数并在webservice中返回响应?
public void myfunction(){
Thread workerThread = Thread.CurrentThread;
bool finished = ExecuteWithTimeLimit(TimeSpan.FromMilliseconds(30000), () =>
{
.....long time execution here....
});
if (!finished)
{
workerThread.Abort();
}
}
public static bool ExecuteWithTimeLimit(TimeSpan timeSpan, Action codeBlock)
{
try
{
Thread workerThread = new Thread(() => codeBlock());
workerThread.Start();
bool finished = workerThread.Join(timeSpan);
return finished;
}
catch (AggregateException ae)
{
throw ae.InnerExceptions[0];
}
}