当您在Visual Studio调试器中打开"抛出异常时断开"功能时,它会在选定的异常类型的任何位置中断.告诉它不要破解特定方法的方法是用DebuggerStepThrough属性(或DebuggerHidden)来装饰这些方法.
这,显然,没有一个工作async由于某种原因的方法.这是一个重现问题的片段.调试器将打破内TestAsync,即使它标明的属性,它会不会打破里面Test的除外(它们之间唯一的区别是第一个标有async关键字):
public class Attributes
{
public async Task Run()
{
await TestAsync();
await Test();
}
[DebuggerHidden]
[DebuggerStepThrough]
public async Task TestAsync()
{
try
{
throw new Exception("Async");
}
catch
{
}
await Task.Delay(100);
}
[DebuggerHidden]
[DebuggerStepThrough]
public Task Test()
{
try
{
throw new Exception("sync");
}
catch
{
}
return Task.Delay(100);
}
}
Run Code Online (Sandbox Code Playgroud)
那么,这种行为是有意的吗?这是一个错误吗?有解决方法吗?
与此问题相关,
是await应该恢复的背景下(特别是通过上下文所代表Thread.CurrentContext的)ContextBoundObject?考虑以下内容:
class Program
{
static void Main(string[] args)
{
var c1 = new Class1();
Console.WriteLine("Method1");
var t = c1.Method1();
t.Wait();
Console.WriteLine("Method2");
var t2 = c1.Method2();
t2.Wait();
Console.ReadKey();
}
}
public class MyAttribute : ContextAttribute
{
public MyAttribute() : base("My") { }
}
[My]
public class Class1 : ContextBoundObject
{
private string s { get { return "Context: {0}"; } } // using a property here, since using a field causes things …Run Code Online (Sandbox Code Playgroud) 所以,我有以下代码:
static void Main(string[] args)
{
var test1 = GeneratedHelperSync();
var test2 = GeneratedHelperAsync().Result;
}
[System.Diagnostics.DebuggerNonUserCode]
static int GeneratedHelperSync()
{
return RealCode();
}
[System.Diagnostics.DebuggerNonUserCode]
async static Task<int> GeneratedHelperAsync()
{
// F11 steps in here!
return await Task.FromResult(RealCode());
}
private static int RealCode()
{
return 1;
}
Run Code Online (Sandbox Code Playgroud)
我是F11(步入)通过Visual Studio 2013中的所有语句.我希望在Main()的两个调用中都可以将我引入"return 1"语句.但是,在第二种情况下,它将我带入异步功能.
似乎编译器在异步代码生成期间删除了我的DebuggerNonUserCode.
我的问题是,为什么这样做?我错过了什么吗?
至于为什么我想要这个,我有一些自动生成的辅助异步函数,我想避免一直踩到它们.