![](/img/trans.png)
[英]Is there a way to handle exceptions thrown by a task without the task freezing the UI?
[英]Where to handle exceptions thrown by Task
我在單獨的任務中執行一些輪詢IO循環。 這些循環可能會遇到異常。 如果遇到異常,我想提醒調用者,以便它可以:
用戶界面必須保持響應。 處理此方案的首選方法是什么? 我在下面列出了一個說明性的程序。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TaskExceptionCatching
{
class Program
{
static void Main(string[] args)
{
startLoops();
System.Console.WriteLine("Type 'Exit' when you're ready to stop.");
while (System.Console.ReadLine() != "Exit")
{
System.Console.WriteLine("Seriously, just type 'Exit' when you're ready to stop.");
}
}
static private void startLoops()
{
System.Console.WriteLine("Starting fizzLoop.");
var fizzTask = Task.Factory.StartNew(new Action(fizzLoop));
System.Console.WriteLine("Starting buzzLoop.");
var buzzTask = Task.Factory.StartNew(new Action(buzzLoop));
}
static private void fizzLoop()
{
while (true)
{
//simulate something error prone, like some risky IO
System.Threading.Thread.Sleep(200);
bool isErr = (new Random().Next(1, 100) == 10);
if (isErr)
throw new Exception("Fizz got an exception.");
}
}
static private void buzzLoop()
{
while (true)
{
//simulate something error prone, like some risky IO
System.Threading.Thread.Sleep(200);
bool isErr = (new Random().Next(1, 100) == 10);
if (isErr)
throw new Exception("Buzz got an exception.");
}
}
}
}
這可能是async void
方法可能很方便的極少數情況之一:
static async void StartAndMonitorAsync(Func<Task> taskFunc)
{
while (true)
{
var task = taskFunc();
try
{
await task;
// process the result if needed
return;
}
catch (Exception ex)
{
// log the error
System.Console.WriteLine("Error: {0}, restarting...", ex.Message);
}
// do other stuff before restarting (if any)
}
}
static private void startLoops()
{
System.Console.WriteLine("Starting fizzLoop.");
StartAndMonitorAsync(() => Task.Factory.StartNew(new Action(fizzLoop)));
System.Console.WriteLine("Starting buzzLoop.");
StartAndMonitorAsync(() => Task.Factory.StartNew(new Action(buzzLoop)));
}
如果你不能使用async/await
,可以使用Task.ContinueWith
實現類似的邏輯。
如果可以多次調用startLoops
(當任務已經“在飛行中”時),則需要使用CancelltionToken
為StartAndMonitorAsync
及其啟動的任務添加取消邏輯(更多細節見“自我取消和重啟的模式”)任務“ )。
從MSDN“任務基礎結構將它們包裝在AggregateException實例中.AggregateException有一個InnerExceptions屬性,可以枚舉該屬性以檢查所有拋出的原始異常,並單獨處理(或不處理)每個異常。即使只有一個例外是拋出,它仍然包含在AggregateException中。“ 有關詳細信息,請查看此信息
try
{
System.Console.WriteLine("Starting fizzLoop.");
var fizzTask = Task.Factory.StartNew(new Action(fizzLoop));
System.Console.WriteLine("Starting buzzLoop.");
var buzzTask = Task.Factory.StartNew(new Action(buzzLoop));
}
catch (AggregateException ae)
{
// Assume we know what's going on with this particular exception.
// Rethrow anything else. AggregateException.Handle provides
// another way to express this. See later example.
foreach (var e in ae.InnerExceptions)
{
if (e is MyCustomException)
{
Console.WriteLine(e.Message);
}
else
{
throw;
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.