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


C# Status.TryTransition方法代码示例

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


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

示例1: TestParallelStateTransitions

        public void TestParallelStateTransitions()
        {
            // The intention here is to cause maximum contention on the Status<> class's methods
            // and thereby provide sufficient confidence that the class provides non-blocking,
            // thread-safe state transitions.
            var control = new
                {
                    NumberOfThreadsPerState = 10,
                    WaitPulseMilliseconds = 200,
                    MainThreadWaitTimeBeforeShutdown = TimeSpan.FromSeconds(10)
                };

            Status<TestStates> state = new Status<TestStates>(default(TestStates));
            int onThreadsStarted = 0;
            int offThreadsStarted = 0;
            int undecidedThreadsStarted = 0;

            int transitionsOn = 0;
            int transitionsOff = 0;
            int transitionsUndecided = 0;
            int transitionsOnStateDone = 0;
            int transitionsOffStateDone = 0;
            int transitionsDone = 0;

            for (int i = 0; i < control.NumberOfThreadsPerState; i++)
            {
                // These background jobs transition the state from TestStates.Undecided
                // to TestStates.On - when successful it increments transitionsOn.
                ThreadPool.QueueUserWorkItem(new WaitCallback((unused_state) =>
                    {
                        state.SpinWaitForState(TestStates.Undecided, () => Thread.Sleep(control.WaitPulseMilliseconds));
                        Interlocked.Increment(ref onThreadsStarted);
                        while (state.IsLessThan(TestStates.ShutdownSignaled))
                        {
                            state.TryTransition(TestStates.On, TestStates.Undecided, () =>
                                {
                                    Interlocked.Increment(ref transitionsOn);
                                });
                        }
                        state.TryTransition(TestStates.OnStateDone, TestStates.ShutdownSignaled, () =>
                            {
                                Interlocked.Increment(ref transitionsOnStateDone);
                            });
                    }));
                // These background jobs transition the state from TestStates.On
                // to TestStates.Off - when successful it increments transitionsOff.
                ThreadPool.QueueUserWorkItem(new WaitCallback((unused_state) =>
                    {
                        state.SpinWaitForState(TestStates.On, () => Thread.Sleep(control.WaitPulseMilliseconds));
                        Interlocked.Increment(ref offThreadsStarted);

                        while(state.IsLessThan(TestStates.OnStateDone))
                        {
                            state.TryTransition(TestStates.Off, TestStates.On, () =>
                                {
                                    Interlocked.Increment(ref transitionsOff);
                                });
                        }
                        state.TryTransition(TestStates.OffStateDone, TestStates.OnStateDone, () =>
                        {
                            Interlocked.Increment(ref transitionsOffStateDone);
                        });
                    }));
                // These background jobs transition the state from TestStates.Off
                // to TestStates.Undecided - when successful it increments transitionsUndecided.
                ThreadPool.QueueUserWorkItem(new WaitCallback((unused_state) =>
                {
                    state.SpinWaitForState(TestStates.Off, () => Thread.Sleep(control.WaitPulseMilliseconds));
                    Interlocked.Increment(ref undecidedThreadsStarted);

                    while (state.IsLessThan(TestStates.OffStateDone))
                    {
                        state.TryTransition(TestStates.Undecided, TestStates.Off, () =>
                        {
                            Interlocked.Increment(ref transitionsUndecided);
                        });
                    }

                    state.TryTransition(TestStates.Done, TestStates.OffStateDone, () =>
                    {
                        Interlocked.Increment(ref transitionsDone);
                    });
                }));
            }

            // Signal the first group of threads that they should start.
            state.SetState(TestStates.Undecided);

            // Wait the prescribed amount of time...
            Thread.Sleep(control.MainThreadWaitTimeBeforeShutdown);
            // Signal the shutdown...
            state.SetState(TestStates.ShutdownSignaled);
            // Wait for background threads to shutdown...
            state.SpinWaitForState(TestStates.Done, () => Thread.Sleep(control.WaitPulseMilliseconds));

            Assert.IsTrue(Thread.VolatileRead(ref transitionsOn) >= Thread.VolatileRead(ref transitionsOff));
            Assert.IsTrue(Thread.VolatileRead(ref transitionsOff) >= Thread.VolatileRead(ref transitionsUndecided));
            Assert.AreEqual(1, Thread.VolatileRead(ref transitionsOnStateDone));
            Assert.AreEqual(1, Thread.VolatileRead(ref transitionsOffStateDone));
            Assert.AreEqual(1, Thread.VolatileRead(ref transitionsDone));
//.........这里部分代码省略.........
开发者ID:lwes,项目名称:lwes-dotnet,代码行数:101,代码来源:StatusTests.cs


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