【发布时间】:2015-06-06 07:30:49
【问题描述】:
代码示例:
using System;
using System.Threading.Tasks;
public class Program
{
public static void Main()
{
Console.WriteLine("Start");
Foo f = new Foo();
f.FooAsync();
}
public class Foo
{
public async void FooAsync()
{
try
{
await Task.Run(() =>
{
Console.WriteLine("Throwing");
throw new Exception();
});
}
catch (Exception)
{
Console.WriteLine("Caught");
}
}
}
}
上面的代码从 C# fiddle 或我的 PC 上的控制台运行时打印:
Start
Throwing
Caught
但是,当我从 Visual Studio(调试模式下按 F5)运行它时,我收到以下消息:
我不明白为什么我从 Visual Studio 收到“未在用户代码中处理”消息,尽管从控制台或 C# fiddle 运行它很好。我错过了什么明显的东西吗?
更新
我已尝试在 Main 中等待任务 f.FooAsync().Wait();,但仍报告异常未处理(相同的错误消息)。
【问题讨论】:
-
代码没问题,是你的调试器设置错误。单击“打开异常设置”链接,取消选中“抛出”复选框。如果您有意打开它,只需点击继续。
-
@HansPassant:谢谢。我找到了more info about the problem here also。
标签: c# .net async-await task-parallel-library