在另一个线程时间方法执行并中止对超时 [英] Time method execution in another thread and abort on timeout
问题描述
您好即时试图以运行一个方法异步时间的持续时间,如果超过了超时取消方法
伊夫试图执行此使用异步和等待。但没有运气。也许即时过度设计此,任何输入将理解
应当注意的是,我不能改变界面TheirInterface(因此名称)
到目前为止的代码:
使用系统;使用System.Diagnostics程序
;
公共接口TheirInterface
{
无效DoHeavyWork();
}
公共类结果
{
公开时间跨度消逝{搞定;组; }
公共异常异常{搞定;组; }
公开结果(时间跨度已过,例外的例外)
{
=经过的经过;
=异常除外;
}
}
公共类TaskTest
{
公共无效启动(TheirInterface implement执行)
{
INT超时= 10000 ;
// TODO
//运行HeavyWorkTimer(implement执行)
//
//等待超时,将> 0
//
//如果发生超时abortheavy
}
公开结果HeavyWorkTimer(implement执行TheirInterface)
{
无功表=新跑表();
试
{
watch.Start();
impl.DoHeavyWork();
watch.Stop();
}
赶上(异常前)
{
watch.Stop();
返回新的结果(watch.Elapsed,除息);
}
返回新的结果(watch.Elapsed,NULL);
}
}
您要创建一个定时器,到期时,将取消该线程。缺席的合作取消,你必须调用 Thread.Abort的
,这是不太理想。我假设你知道参与中止线程的危险。
公开结果HeavyWorkTimer(implement执行TheirInterface)
{
无功表=新的秒表();
异常savedException = NULL;
变种的WorkerThread =新的Thread(()=>
{
试
{
watch.Start();
implement执行。 DoHeavyWork();
watch.Stop();
}
赶上(异常前)
{
watch.Stop();
savedException =前;
}
});
//创建一个定时器5秒后杀的工作线程。
变种timeoutTimer =新System.Threading.Timer((S)=>
{
workerThread.Abort();
},空TimeSpan.FromSeconds(5), TimeSpan.Infinite);
workerThread.Start();
workerThread.Join();
返回新的结果(watch.Elapsed,savedException);
}
请注意,如果您中止线程,该线程将获得 ThreadAbortException
。
如果你能说服接口车主加合作取消,那么你就可以使其请求取消改变定时器的回调在令牌,而不是调用 Thread.Abort的
。
Hi im trying to run a method async in order to time the duration and cancel the method if a timeout is exceeded.
Ive tried to implement this using async and await. But with no luck. Perhaps im overengineering this, any inputs will be appreciated
It should be noted that i cannot change the interface "TheirInterface" (Hence the name)
Code so far:
using System;
using System.Diagnostics;
public interface TheirInterface
{
void DoHeavyWork();
}
public class Result
{
public TimeSpan Elapsed { get; set; }
public Exception Exception { get; set; }
public Result(TimeSpan elapsed, Exception exception)
{
Elapsed = elapsed;
Exception = exception;
}
}
public class TaskTest
{
public void Start(TheirInterface impl)
{
int timeout = 10000;
// TODO
// Run HeavyWorkTimer(impl)
//
// Wait for timeout, will be > 0
//
// if timeout occurs abortheavy
}
public Result HeavyWorkTimer(TheirInterface impl)
{
var watch = new Stopwatch();
try
{
watch.Start();
impl.DoHeavyWork();
watch.Stop();
}
catch (Exception ex)
{
watch.Stop();
return new Result(watch.Elapsed, ex);
}
return new Result(watch.Elapsed, null);
}
}
You want to create a timer that, when it expires, cancels the thread. Absent cooperative cancellation, you'll have to call Thread.Abort
, which is less than ideal. I'll assume that you know of the dangers involved in aborting threads.
public Result HeavyWorkTimer(TheirInterface impl)
{
var watch = new Stopwatch();
Exception savedException = null;
var workerThread = new Thread(() =>
{
try
{
watch.Start();
impl.DoHeavyWork();
watch.Stop();
}
catch (Exception ex)
{
watch.Stop();
savedException = ex;
}
});
// Create a timer to kill the worker thread after 5 seconds.
var timeoutTimer = new System.Threading.Timer((s) =>
{
workerThread.Abort();
}, null, TimeSpan.FromSeconds(5), TimeSpan.Infinite);
workerThread.Start();
workerThread.Join();
return new Result(watch.Elapsed, savedException);
}
Note that if you abort the thread, the thread will get ThreadAbortException
.
If you can convince the interface owner to add cooperative cancellation, then you can change the timer callback so that it requests cancellation on the token rather than calling Thread.Abort
.
这篇关于在另一个线程时间方法执行并中止对超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!