C# 在任务中捕获异常的最佳方法是什么?
使用C# 在任务中捕获异常的最佳方法是什么?,c#,.net,task-parallel-library,C#,.net,Task Parallel Library,使用System.Threading.Tasks.Task,我必须管理可能引发的异常。我在寻找最好的方法。到目前为止,我已经创建了一个基类来管理.ContinueWith(…) 我想知道有没有更好的办法。即使这是一个很好的方法 public class BaseClass { protected void ExecuteIfTaskIsNotFaulted<T>(Task<T> e, Action action) { if (!e.IsFa
System.Threading.Tasks.Task
,我必须管理可能引发的异常。我在寻找最好的方法。到目前为止,我已经创建了一个基类来管理.ContinueWith(…)
我想知道有没有更好的办法。即使这是一个很好的方法
public class BaseClass
{
protected void ExecuteIfTaskIsNotFaulted<T>(Task<T> e, Action action)
{
if (!e.IsFaulted) { action(); }
else
{
Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() =>
{
/* I display a window explaining the error in the GUI
* and I log the error.
*/
this.Handle.Error(e.Exception);
}));
}
}
}
public class ChildClass : BaseClass
{
public void DoItInAThread()
{
var context = TaskScheduler.FromCurrentSynchronizationContext();
Task.Factory.StartNew<StateObject>(() => this.Action())
.ContinueWith(e => this.ContinuedAction(e), context);
}
private void ContinuedAction(Task<StateObject> e)
{
this.ExecuteIfTaskIsNotFaulted(e, () =>
{
/* The action to execute
* I do stuff with e.Result
*/
});
}
}
公共类基类
{
受保护的void executeIftaskisnotfault(任务e,操作)
{
如果(!e.IsFaulted){action();}
其他的
{
Dispatcher.CurrentDispatcher.BeginInvoke(新操作(()=>
{
/*我会在GUI中显示一个解释错误的窗口
*然后我记录错误。
*/
this.Handle.Error(例如异常);
}));
}
}
}
公共类子类:基类
{
公共无效线程()
{
var context=TaskScheduler.FromCurrentSynchronizationContext();
Task.Factory.StartNew(()=>this.Action())
.ContinueWith(e=>this.continueAction(e),上下文);
}
私有void ContinuedAction(任务e)
{
此.executeIftaskisnotfault(e,()=>
{
/*要执行的操作
*我用e.Result做东西
*/
});
}
}
根据您使用的语言版本,有两种方法可以做到这一点
C#5.0及以上
您可以使用和关键字来简化这方面的大量工作
async
和await
被引入到该语言中,以简化的使用,防止您不得不使用,并允许您继续以自顶向下的方式编程
因此,您可以简单地使用块捕获异常,如下所示:
try
{
// Start the task.
var task = Task.Factory.StartNew<StateObject>(() => { /* action */ });
// Await the task.
await task;
}
catch (Exception e)
{
// Perform cleanup here.
}
// Get the task.
var task = Task.Factory.StartNew<StateObject>(() => { /* action */ });
// For error handling.
task.ContinueWith(t => { /* error handling */ }, context,
TaskContinuationOptions.OnlyOnFaulted);
TaskContinuationOptions
枚举的OnlyOnFaulted
成员指示,只有在先行任务引发异常时才应执行继续
当然,您可以在同一个先行项下调用多个ContinueWith
,以处理非异常情况:
// Get the task.
var task = new Task<StateObject>(() => { /* action */ });
// For error handling.
task.ContinueWith(t => { /* error handling */ }, context,
TaskContinuationOptions.OnlyOnFaulted);
// If it succeeded.
task.ContinueWith(t => { /* on success */ }, context,
TaskContinuationOptions.OnlyOnRanToCompletion);
// Run task.
task.Start();
//获取任务。
var task=新任务(()=>{/*操作*/});
//用于错误处理。
task.ContinueWith(t=>{/*错误处理*/},上下文,
TaskContinuationOptions.OnlyOnFaulted);
//如果成功了。
task.ContinueWith(t=>{/*on success*/},context,
TaskContinuationOptions.OnlyOnRanToCompletion);
//运行任务。
task.Start();
您可以创建一些自定义任务工厂,它将生成嵌入异常处理的任务。大概是这样的:
using System;
using System.Threading.Tasks;
class FaFTaskFactory
{
public static Task StartNew(Action action)
{
return Task.Factory.StartNew(action).ContinueWith(
c =>
{
AggregateException exception = c.Exception;
// Your Exception Handling Code
},
TaskContinuationOptions.OnlyOnFaulted | TaskContinuationOptions.ExecuteSynchronously
).ContinueWith(
c =>
{
// Your task accomplishing Code
},
TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.ExecuteSynchronously
);
}
public static Task StartNew(Action action, Action<Task> exception_handler, Action<Task> completion_handler)
{
return Task.Factory.StartNew(action).ContinueWith(
exception_handler,
TaskContinuationOptions.OnlyOnFaulted | TaskContinuationOptions.ExecuteSynchronously
).ContinueWith(
completion_handler,
TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.ExecuteSynchronously
);
}
};
但如果说实话,我真的不知道你为什么想要完成处理代码。无论如何,此决定取决于应用程序的逻辑。您如何知道匿名方法中的异常类型?如果我没有。异常,intellisense不会公开innerexception、message…等属性…@guiomie
t
是异常。上下文未定义。它是什么?@MonsterMMORPGSynchronizationContext
,如果需要。Ty回答我们为什么需要它?我是说在那种情况下?这够了吗?myTask.ContinueWith(t=>ErrorLogger.LogError(“在func_CheckWaitingToProcessPages启动任务时发生错误,错误:+t),TaskContinuationOptions.OnlyOnFaulted);
var task1 = FaFTaskFactory.StartNew( () => { throw new NullReferenceException(); } );
var task2 = FaFTaskFactory.StartNew( () => { throw new NullReferenceException(); },
c => { Console.WriteLine("Exception!"); },
c => { Console.WriteLine("Success!" ); } );
task1.Wait(); // You can omit this
task2.Wait(); // You can omit this