【问题标题】:ThreadStatic for .net compact framework.net 紧凑框架的 ThreadStatic
【发布时间】:2017-08-09 12:49:50
【问题描述】:

我们如何在 .net compact framework 3.5 中重现 ThradStatic 行为?

【问题讨论】:

    标签: c# multithreading compact-framework


    【解决方案1】:

    我建议一个静态(在本例中为 appdomain 静态)字典,由线程 ID 索引,并使用 Thread.Current 将相关的 getter 和 setter 索引到字典中。

    【讨论】:

      【解决方案2】:

      您可以使用LocalDataStoreSlot 类,它会在线程终止时自动丢弃对象。

      例如:

      private static readonly LocalDataStoreSlot nameSlot = Thread.AllocateDataSlot();
      
      public string Name
      {
          get { return (string)Thread.GetData(nameSlot); }
          set { Thread.SetData(nameSlot, value); }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-10-17
        • 2010-09-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多