加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 将ThreadStatic字段与Task一起使用

发布时间:2020-12-15 21:44:24 所属栏目:百科 来源:网络整理
导读:开始编辑 执行此操作(4.5之前)的“正确”方法是使用此处概述的SynchronizationContext:http://msdn.microsoft.com/en-us/magazine/gg598924.aspx 我相信在4.5中使用async / await关键字自动处理SynchronizationContext,但是没有做足够的工作来确认. 在4.0中
开始编辑

执行此操作(4.5之前)的“正确”方法是使用此处概述的SynchronizationContext:http://msdn.microsoft.com/en-us/magazine/gg598924.aspx

我相信在4.5中使用async / await关键字自动处理SynchronizationContext,但是没有做足够的工作来确认.

在4.0中,您可能希望利用“TaskScheduler.FromCurrentSynchronizationContext”来捕获当前线程的上下文(在我的情况下,这将包括HttpContext,但BCL的各个部分提供类似的构造).

结束编辑

主要问题是:

以下是与任务共享“上下文”的“安全”机制吗?

在我的用例中,没有太多细节,不可能直接将上下文推送到操作中.

public static class ContextCaller{
    [ThreadStatic]
    public static object SharedState;

    public static Task InvokeWithContext(this Action theAction){
        //We're still running in the "outer" context,//so we can collect a variable and store it in the thread-static
        //field by closing it into the task.
        var context = new object();

        var t = new Task(()=>{
            try{
                //close in the context
                SharedState = context;
                theAction();
            }
            finally{
                //teardown the shared state.
                SharedState = null;
            }
        });
        t.Start();
        return t;
    }
}

现在客户端代码:

//client code:
Action doWork = ()=>{
    var state = ContextCaller.SharedState;
    //do work on state,potentially throwing an exception in the process.
};

//cause the task to be invoked with some data available only on this thread.
doWork.InvokeWithContext();

根据我对一个任务和一个线程之间关系的理解,上面应该是安全的,因为:

>一个任务将在一个线程上运行(假设该操作不会产生额外的任务/线程).
> ThreadStatic字段已设置
在执行“theAction()”之前,“finally”保证此字段为
无论结果如何,在调用“theAction()”之后重置.

除了明确地将参数关闭到“theAction”之外,还有其他更好的模式来定义任务上下文吗?

解决方法

我没有看到任何技术原因导致您的代码无法正常工作.

但我也认为这样做是不好的做法,只有在没有其他方法的情况下才会使用这样的东西.并且我认为添加另一个方法的重载,它采用Action< object> (或者甚至更好,使用强类型参数)不应该是一个重大的变化.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读