小编Ash*_*win的帖子

如何在C#中跨异步等待模型维护线程上下文?

每次等待完成"选项"时,是否正在使用ThreadStatic并设置上下文?还有另外一种方法吗?

public async void Test()
{
    // This is in Thread 1
    Foo foo = new Foo();
    Context.context = "context1"; // This is ThreadStatic
    string result = await foo.CallAsynx();

    // This is most likely Thread 2
    Context.context = "context1";   // This might be a different thread and so resetting context    
}
Run Code Online (Sandbox Code Playgroud)

如果我不想使用ThreadStatic,现在有另一种方法吗?

c# threadstatic async-await

3
推荐指数
2
解决办法
5505
查看次数

标签 统计

async-await ×1

c# ×1

threadstatic ×1