相关疑难解决方法(0)

DebuggerStepThrough,DebuggerHidden在async-await方法中不起作用

当您在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)

那么,这种行为是有意的吗?这是一个错误吗?有解决方法吗?

.net c# debugging exception-handling async-await

11
推荐指数
1
解决办法
1085
查看次数

等待应该恢复Thread.CurrentContext?

此问题相关,

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)

c# async-await

7
推荐指数
1
解决办法
1656
查看次数

DebuggerNonUserCode不适用于async/await

所以,我有以下代码:

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.
我的问题是,为什么这样做?我错过了什么吗?

至于为什么我想要这个,我有一些自动生成的辅助异步函数,我想避免一直踩到它们.

c# attributes visual-studio async-await

6
推荐指数
1
解决办法
302
查看次数