【发布时间】:2010-10-14 05:20:41
【问题描述】:
如果异步任务处理时间过长,我刚刚编写了一些代码来执行超时操作,但我不清楚是否以及何时处理超时实例(我认为它会在异步任务及时完成的情况下完成,否则我不知道),或者我每次调用这段代码时都要累积实例。
//StartNew creates a new instance of System.Timers.Timer, and
// invokes the ActionOnTimeout after 2000ms, unless calling code
// calls "Stop" first
var timeout = ProcessTimeout.StartNew(() => ActionOnTimeout(), 2000);
//DoAsyncTask creates a new thread, does potentially slow stuff,
/// then invokes this callback
DoAsyncTask(() =>
{
if(timeout.Running)
{
timeout.Stop();
DoCallbackStuff();
}
});
(如果有帮助,ProcessTimeout 类使用System.Timers.Timer)
【问题讨论】:
标签: c# garbage-collection lambda closures