Does the thread identity get transferred when using PLINQ Extensions?

后端 未结 3 1492
伪装坚强ぢ
伪装坚强ぢ 2021-02-15 13:51

I am using .AsParallel().ForAll() to enumerate a collection in parallel in the context of an ASP.NET request. The enumeration method relies on System.Threading.Thread.CurrentPr

3条回答
  •  遇见更好的自我
    2021-02-15 14:25

    This is the implementation behind CurrentPrincipal

    public static IPrincipal CurrentPrincipal
    {
        get
        {
            lock (CurrentThread)
            {
                IPrincipal threadPrincipal = CallContext.Principal;
                if (threadPrincipal == null)
                {
                    threadPrincipal = GetDomain().GetThreadPrincipal();
                    CallContext.Principal = threadPrincipal;
                }
                return threadPrincipal;
            }
        }
        set { CallContext.Principal = value; }
    }
    

    All newly created threads will have null and it will be taken from application domain. So it should be ok. Nevertheless you need be careful with culture. It will not be derived from starting thread. See: Parallel Programing, PLINQ and Globalization

提交回复
热议问题