我正在通过以下方法在后台线程池线程上启动任务
private async Task LoadCoreMatchDataAsync()
{
string errorMessage = String.Empty;
...
try
{
if (!HasConnection)
return;
IProgress<string> progressIndicator = new Progress<string>(LoadProgress);
EventsCollection = new BindableCollection<Taurus.MatchDetails>(
await MatchDataService.GetCollectionAsync(
this.client, progressIndicator, this.token));
...
}
catch (TimeoutException toe)
{
errorMessage = String.Format(
"Retrieval of the MatchDetails using connection " +
"\"{0}\" failed with the following TimeoutException: \"{1}\".",
this.ConnectionString,
toe.Message);
}
catch (OperationCanceledException)
{
// Do nothing, cancel silently.
}
// Display any errors.
if (!String.IsNullOrEmpty(errorMessage))
{
await dialogManager.ShowDialog<MessageDialogResult>(
new MessageBoxViewModel("Connection Timeout", errorMessage, mbs));
HasConnection = false;
}
}
GetCollectionAsync(...)
方法在哪里
public async Task<BindableCollection<Taurus.MatchDetails>> GetCollectionAsync(
MongoClient client, IProgress<string> progressIndicator, CancellationToken token)
{
return await Task.Factory.StartNew<BindableCollection<Taurus.MatchDetails>>(() =>
{
... // Somewhere in here we get a TimeoutException thrown.
}, token);
}
问题是,当我调用 await MatchDataService.GetCollectionAsync(...)
时,我得到一个expected TimeoutException
,VS2012 抛出“用户代码未处理 TimeoutException”消息,显然我正在以正确的方式处理“继续”中的异常。如果我继续而不是中断,异常确实被捕获并且我得到了我预期的错误消息。我只是不确定为什么 VS2012 告诉我异常未处理?
我基本上是在做 Jon Skeets 在这里的回答中清楚描述的事情 https://stackoverflow.com/a/19865613/626442 .
感谢您的宝贵时间。
最佳答案
您打开了“仅我的代码”(我在您引用的另一个问题的回答中提到了这一点)。调试器表明“用户代码”(您的代码)没有处理异常——这是真的。异常由框架代码捕获并放入任务中。
关闭“Just My Code”调试器设置(在我看来,这是一个只会引起混淆并且用处非常有限的功能)。
关于c# - 线程池线程抛出的异常显示为未处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28581398/