我必须在一个线程中执行一个长进程操作并通过将结果返回给一个函数来继续。这是我的代码:
Task<ProductEventArgs>.Factory.StartNew(() =>
{
try
{
// long operation which return new ProductEventArgs with a list of product
}
catch (Exception e)
{
return new ProductEventArgs() { E = e };
}
}).ContinueWith((x) => handleResult(x.Result), TaskScheduler.FromCurrentSynchronizationContext());
问题实际上是我没有超时。我想放置一个计时器以返回如下内容:
new ProductEventArgs() { E = new Exception("timeout") };
如果达到超时。不能使用等待/异步。非常感谢 !