【发布时间】:2021-04-09 23:09:32
【问题描述】:
下面的代码有一个TaskMonitor 类,它是Stephen Cleary 的NotifyTask class 的修改版本。 Worker 类使用 TaskMonitor 类异步运行 Worker.CheckStatus。
Worker.CheckStatus 经常会抛出异常(例如数据库连接问题)。我希望 TaskMonitor.Monitor 阻止该异常到达 Program.Main 并通过 Exception 和 InnerException 属性公开它。 TaskMonitor.Monitor 中的 catch 块被命中但没有按预期吞下异常;异常被传递给 Program.Main 并使应用程序崩溃。如何停止 TaskMonitor.Monitor 中的异常?
public sealed class TaskMonitor {
// Based on Stephen Cleary's NotifyTaskCompletion:
// https://github.com/StephenCleary/Mvvm/blob/master/future/Nito.Mvvm.Async/NotifyTask.cs
public TaskMonitor(Task task = null) {
Task = task;
}
public AggregateException Exception {
get {
return Task?.Exception;
}
}
public Exception InnerException {
get {
return Exception?.InnerException;
}
}
public bool IsCompleted {
get {
return Task != null && Task.IsCompleted;
}
}
public bool IsRunning {
get {
return Task != null && !Task.IsCompleted;
}
}
private async Task Monitor(Task task) {
try {
if(task != null) {
await task;
}
}
catch {
// Drop exceptions
}
finally {
// do other stuff here like raise PropertyChanged
}
}
private Task _task;
public Task Task {
get => _task;
set {
if(Task != value) {
_task = value;
_ = Monitor(Task);
}
}
}
}
public class Worker {
public Worker() {
CheckStatusMonitor = new TaskMonitor();
}
public void CheckStatus() {
if(!CheckStatusMonitor.IsRunning) {
CheckStatusMonitor.Task = Task.Run(async () => {
var newStatus = WorkerStatus.Unknown;
try {
// Force a database connection failure
var cn = new SqlConnection();
await cn.OpenAsync();
newStatus = WorkerStatus.Good;
}
catch(Exception ex) {
newStatus = WorkerStatus.Error;
throw ex;
}
finally {
Status = newStatus;
}
});
}
}
public TaskMonitor CheckStatusMonitor { get; private set; }
public WorkerStatus Status { get; private set; }
}
public enum WorkerStatus {
Unknown,
Error,
Good,
}
class Program {
public static async Task Main() {
var worker = new Worker();
worker.CheckStatus();
await worker.CheckStatusMonitor.Task;
Debug.WriteLine(worker.CheckStatusMonitor.ErrorMessage);
}
}
【问题讨论】:
-
您将
_task设置为从Task.Run返回的Task,而不是从Monitor返回的Task。然后,您在await worker.CheckStatusMonitor.Task;中等待
标签: c# exception async-await