我必須在一個線程中執行一個長進程操作並繼續將結果返回給一個函數。這裏是我的代碼:C#任務工廠超時
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") };
如果超時已達到。 不能使用await/async。 非常感謝!
愛你: - *我在C#有點新的,更多的線程管理,你讓我的天! – Rototo