当前位置: 首页>>代码示例>>C#>>正文


C# ThreadLocal.Dispose方法代码示例

本文整理汇总了C#中ThreadLocal.Dispose方法的典型用法代码示例。如果您正苦于以下问题:C# ThreadLocal.Dispose方法的具体用法?C# ThreadLocal.Dispose怎么用?C# ThreadLocal.Dispose使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ThreadLocal的用法示例。


在下文中一共展示了ThreadLocal.Dispose方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: PlayWithThreadLocal

        public void PlayWithThreadLocal()
        {
            var threadName = new ThreadLocal<string>(() => Thread.CurrentThread.ManagedThreadId.ToString());
            Action action = () =>
            {
                var repeat = threadName.IsValueCreated ? "(repeat)" : "";
                var name = threadName.Value;
                Console.Out.WriteLine("name = {0} {1}", name, repeat);
            };

            Parallel.Invoke(action, action, action, action, action, action, action, action);

            threadName.Dispose();
        }
开发者ID:ahazelwood,项目名称:EasyNetQ,代码行数:14,代码来源:ThreadLocalSpike.cs

示例2: DisposedOnValueTest

 public void DisposedOnValueTest()
 {
     var tl = new ThreadLocal<int>();
     tl.Dispose();
     var value = tl.Value;
 }
开发者ID:mesheets,项目名称:Theraot-CF,代码行数:6,代码来源:ThreadLocalTests.cs

示例3: DisposedOnIsValueCreatedTest

 public void DisposedOnIsValueCreatedTest()
 {
     var tl = new ThreadLocal<int>();
     tl.Dispose();
     var value = tl.IsValueCreated;
 }
开发者ID:mesheets,项目名称:Theraot-CF,代码行数:6,代码来源:ThreadLocalTests.cs

示例4: RunThreadLocalTest5_Dispose

        private static bool RunThreadLocalTest5_Dispose()
        {
            TestHarness.TestLog("* RunThreadLocalTest5_Dispose()");

            ThreadLocal<string> tl = new ThreadLocal<string>(() => "dispose test");
            string value = tl.Value;

            tl.Dispose();

            if (!TestHarnessAssert.EnsureExceptionThrown(() => { string tmp = tl.Value; }, typeof(ObjectDisposedException), "The Value property of the disposed ThreadLocal object should throw ODE"))
                return false;

            if (!TestHarnessAssert.EnsureExceptionThrown(() => { bool tmp = tl.IsValueCreated; }, typeof(ObjectDisposedException), "The IsValueCreated property of the disposed ThreadLocal object should throw ODE"))
                return false;

            if (!TestHarnessAssert.EnsureExceptionThrown(() => { string tmp = tl.ToString(); }, typeof(ObjectDisposedException), "The ToString method of the disposed ThreadLocal object should throw ODE"))
                return false;


            // test recycling the combination index;

            tl = new ThreadLocal<string>(() => null);
            if(tl.IsValueCreated)
            {
                TestHarness.TestLog("* Filed, reusing the same index kept the old value and didn't use the new value.");
                return false;
            }
            if (tl.Value != null)
            {
                TestHarness.TestLog("* Filed, reusing the same index kept the old value and didn't use the new value.");
                return false;
            }


            return true;
        }
开发者ID:modulexcite,项目名称:IL2JS,代码行数:36,代码来源:ThreadLocalTests.cs

示例5: RunThreadLocalTest8Helper

        private static void RunThreadLocalTest8Helper(ManualResetEventSlim mres)
        {
            var tl = new ThreadLocal<object>(true);
            var t = Task.Run(() => tl.Value = new SetMreOnFinalize(mres));
            t.Wait();
            t = null;
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            Assert.True(tl.Values.Count == 1, "RunThreadLocalTest8_Values: Expected other thread to have set value");
            Assert.True(tl.Values[0] is SetMreOnFinalize, "RunThreadLocalTest8_Values: Expected other thread's value to be of the right type");
            tl.Dispose();

            object values;
            Assert.Throws<ObjectDisposedException>(() => values = tl.Values);
        }
开发者ID:noahfalk,项目名称:corefx,代码行数:16,代码来源:ThreadLocalTests.cs

示例6: RunThreadLocalTest8_Values

        public static void RunThreadLocalTest8_Values()
        {
            // Test adding values and updating values
            {
                var threadLocal = new ThreadLocal<int>(true);
                Assert.True(threadLocal.Values.Count == 0, "RunThreadLocalTest8_Values: Expected thread local to initially have 0 values");
                Assert.True(threadLocal.Value == 0, "RunThreadLocalTest8_Values: Expected initial value of 0");
                Assert.True(threadLocal.Values.Count == 1, "RunThreadLocalTest8_Values: Expected values count to now be 1 from initialized value");
                Assert.True(threadLocal.Values[0] == 0, "RunThreadLocalTest8_Values: Expected values to contain initialized value");

                threadLocal.Value = 42;
                Assert.True(threadLocal.Values.Count == 1, "RunThreadLocalTest8_Values: Expected values count to still be 1 after updating existing value");
                Assert.True(threadLocal.Values[0] == 42, "RunThreadLocalTest8_Values: Expected values to contain updated value");

                ((IAsyncResult)Task.Run(() => threadLocal.Value = 43)).AsyncWaitHandle.WaitOne();
                Assert.True(threadLocal.Values.Count == 2, "RunThreadLocalTest8_Values: Expected values count to be 2 now that another thread stored a value");
                Assert.True(threadLocal.Values.Contains(42) && threadLocal.Values.Contains(43), "RunThreadLocalTest8_Values: Expected values to contain both thread's values");

                int numTasks = 1000;
                Task[] allTasks = new Task[numTasks];
                for (int i = 0; i < numTasks; i++)
                {
                    // We are creating the task using TaskCreationOptions.LongRunning because...
                    // there is no guarantee that the Task will be created on another thread.
                    // There is also no guarantee that using this TaskCreationOption will force
                    // it to be run on another thread.
                    var task = Task.Factory.StartNew(() => threadLocal.Value = i, CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default);
                    task.Wait();
                }

                var values = threadLocal.Values;
                Assert.True(values.Count == 1002, "RunThreadLocalTest8_Values: Expected values to contain both previous values and 1000 new values");
                for (int i = 0; i < 1000; i++)
                {
                    Assert.True(values.Contains(i), "RunThreadLocalTest8_Values: Expected values to contain value for thread #: " + i);
                }

                threadLocal.Dispose();
            }

            // Test that thread values remain after threads depart
            {
                var tl = new ThreadLocal<string>(true);
                var t = Task.Run(() => tl.Value = "Parallel");
                t.Wait();
                t = null;
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                Assert.True(tl.Values.Count == 1, "RunThreadLocalTest8_Values: Expected values count to be 1 from other thread's initialization");
                Assert.True(tl.Values.Contains("Parallel"), "RunThreadLocalTest8_Values: Expected values to contain 'Parallel'");
            }
        }
开发者ID:noahfalk,项目名称:corefx,代码行数:53,代码来源:ThreadLocalTests.cs

示例7: RunThreadLocalTest5_Dispose_Negative

        public static void RunThreadLocalTest5_Dispose_Negative()
        {
            ThreadLocal<string> tl = new ThreadLocal<string>(() => "dispose test");
            string value = tl.Value;

            tl.Dispose();

            Assert.Throws<ObjectDisposedException>(() => { string tmp = tl.Value; });
            // Failure Case: The Value property of the disposed ThreadLocal object should throw ODE

            Assert.Throws<ObjectDisposedException>(() => { bool tmp = tl.IsValueCreated; });
            // Failure Case: The IsValueCreated property of the disposed ThreadLocal object should throw ODE

            Assert.Throws<ObjectDisposedException>(() => { string tmp = tl.ToString(); });
            // Failure Case: The ToString method of the disposed ThreadLocal object should throw ODE

        }
开发者ID:noahfalk,项目名称:corefx,代码行数:17,代码来源:ThreadLocalTests.cs


注:本文中的ThreadLocal.Dispose方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。