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


C# WorkerThread.Stop方法代码示例

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


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

示例1: DoWorkShouldNotCallWorkItemDequeueWhenWorkerThreadManagerShouldExitWorkerThreadReturnsTrue

        public void DoWorkShouldNotCallWorkItemDequeueWhenWorkerThreadManagerShouldExitWorkerThreadReturnsTrue()
        {
            WorkerThreadManagerStub workerThreadManager = WorkerThreadManagerStub.Create(); 
            
            ManualResetEvent manualResetEvent = new ManualResetEvent(false);
            IWorkItemQueue workItemQueue = Substitute.For<IWorkItemQueue>();
            WorkerThread workerThread = new WorkerThread(workerThreadManager, workItemQueue, "Test", false);
            workerThreadManager.WorkerThreadExiting += (sender, args) =>
                {
                    if (args.WorkerThreadExitReason == WorkerThreadExitReason.MaximumThreadCountExceeded)
                    {
                        manualResetEvent.Set();
                    }
                };
            workerThreadManager.ShouldExitWorkerThread(workerThread, false).ReturnsForAnyArgs(true);
            workerThread.Start();

            manualResetEvent.WaitOne(500);

            workerThread.Stop();

            Wait.While(() => workerThread.Thread.IsAlive, 300);

            Assert.AreEqual(false, workerThread.Thread.IsAlive);

            workItemQueue.DidNotReceive().Dequeue();
        }
开发者ID:QuickOrBeDead,项目名称:Labo.Threading,代码行数:27,代码来源:WorkerThreadFixture.cs

示例2: StartShouldStartThread

        public void StartShouldStartThread()
        {
            WorkerThreadManagerStub workerThreadManager = WorkerThreadManagerStub.Create(); 

            IWorkItemQueue workItemQueue = Substitute.For<IWorkItemQueue>();
            workItemQueue.Dequeue().Returns((IWorkItem)null);

            WorkerThread workerThread = new WorkerThread(workerThreadManager, workItemQueue, "Test", false);
            workerThreadManager.ShouldExitWorkerThread(workerThread, false).Returns(false);            
            workerThread.Start();

            Wait.While(() => !workerThread.Thread.IsAlive, 300);

            Assert.AreEqual(true, workerThread.Thread.IsAlive);

            workerThread.Stop();

            Wait.While(() => workerThread.Thread.IsAlive, 300);

            Assert.AreEqual(false, workerThread.Thread.IsAlive);
        }
开发者ID:QuickOrBeDead,项目名称:Labo.Threading,代码行数:21,代码来源:WorkerThreadFixture.cs

示例3: SerialRequestsOnWorker

		async Task SerialRequestsOnWorker ()
		{
			WriteLine ("Starting serial requests on worker thread.");

			var worker = new WorkerThread ();
			var handler = new MessageHandler (worker);
			var client = new HttpClient (handler, true);

			var start = DateTime.Now;

			try {
				for (int i = 0; i < repeatCount; i++) {
					await CreateTask (client);
				}
			} finally {
				client.Dispose ();
				worker.Stop ();
			}

			var time = DateTime.Now - start;
			var perRequest = (int)(time.TotalMilliseconds / repeatCount);

			WriteLine ("Serial requests on worker thread done in {0} ({1}ms/request).",
			           time, perRequest);
		}
开发者ID:RafasTavares,项目名称:mac-samples,代码行数:25,代码来源:BenchmarkRunner.cs

示例4: DoWorkShouldCallWorkItemDequeueWhenWorkerThreadManagerShouldExitWorkerThreadReturnsFalse

        public void DoWorkShouldCallWorkItemDequeueWhenWorkerThreadManagerShouldExitWorkerThreadReturnsFalse()
        {
            WorkerThreadManagerStub workerThreadManager = WorkerThreadManagerStub.Create(); 

            ManualResetEvent manualResetEvent = new ManualResetEvent(false);
            IWorkItemQueue workItemQueue = Substitute.For<IWorkItemQueue>();
            workItemQueue.When(x => x.Dequeue()).Do(x => { manualResetEvent.Set(); });
           
            WorkerThread workerThread = new WorkerThread(workerThreadManager, workItemQueue, "Test", false);
            workerThreadManager.ShouldExitWorkerThread(workerThread, true).Returns(false);
            workerThread.StopWhenWorkItemQueueIsEmpty(true);
            workerThread.Start();

            manualResetEvent.WaitOne(500);
            workerThread.Stop();

            Wait.While(() => workerThread.Thread.IsAlive, 300);

            Assert.AreEqual(false, workerThread.Thread.IsAlive);

            workItemQueue.Received(1).Dequeue();
        }
开发者ID:QuickOrBeDead,项目名称:Labo.Threading,代码行数:22,代码来源:WorkerThreadFixture.cs

示例5: StopShouldCallCurrentWorkItemStopMethodWhenCalledDuringAnyWorkItemIsInProgress

        public void StopShouldCallCurrentWorkItemStopMethodWhenCalledDuringAnyWorkItemIsInProgress()
        {
            WorkerThreadManagerStub workerThreadManager = WorkerThreadManagerStub.Create();

            ManualResetEvent startWaitHandle = new ManualResetEvent(false);
            ManualResetEvent stopWaitHandle = new ManualResetEvent(false);
            IWorkItemQueue workItemQueue = Substitute.For<IWorkItemQueue>();
            IWorkItem workItem = Substitute.For<IWorkItem>();
            workItem.When(x => x.DoWork()).Do(
                x =>
                    {
                        startWaitHandle.Set();
                        stopWaitHandle.WaitOne(500);
                    });
            workItemQueue.Dequeue().Returns(workItem);

            WorkerThread workerThread = new WorkerThread(workerThreadManager, workItemQueue, "Test", false);
            workerThread.Start();

            startWaitHandle.WaitOne(500);
            workerThread.Stop();
            stopWaitHandle.Set();

            Wait.While(() => workerThread.Thread.IsAlive, 500);

            Assert.AreEqual(false, workerThread.Thread.IsAlive);

            workItem.Received(1).Stop();
        }
开发者ID:QuickOrBeDead,项目名称:Labo.Threading,代码行数:29,代码来源:WorkerThreadFixture.cs

示例6: DoWorkShouldCallWorkerThreadExceptionEventWhenExceptionOccursOutsideOfWorkItemDoWorkMethod

        public void DoWorkShouldCallWorkerThreadExceptionEventWhenExceptionOccursOutsideOfWorkItemDoWorkMethod()
        {
            WorkerThreadManagerStub workerThreadManager = WorkerThreadManagerStub.Create(); 

            ManualResetEvent manualResetEvent = new ManualResetEvent(false);
            IWorkItemQueue workItemQueue = Substitute.For<IWorkItemQueue>();
            IWorkItem workItem = Substitute.For<IWorkItem>();
            workItemQueue.Dequeue().Returns(workItem);

            Exception invalidCastException = new InvalidCastException();
            Exception otherThreadsException = null;
            bool workerThreadExitingEventIsCalled = false;
            WorkerThread workerThread = new WorkerThread(workerThreadManager, workItemQueue, "Test", false);
            workerThreadManager.WorkerThreadStarted += (sender, args) =>
                {
                    throw invalidCastException;
                };
            workerThreadManager.WorkerThreadException += (sender, args) =>
                {
                    try
                    {
                        Assert.AreEqual(invalidCastException, args.Exception);
                    }
                    catch (Exception ex)
                    {
                        otherThreadsException = ex;
                    }
                };
            workerThreadManager.WorkerThreadExiting += (sender, args) =>
            {
                workerThreadExitingEventIsCalled = true;

                try
                {
                    Assert.AreEqual(WorkerThreadExitReason.ExceptionOccurred, args.WorkerThreadExitReason);
                }
                catch (Exception ex)
                {
                    otherThreadsException = ex;
                }

                manualResetEvent.Set();
            };
            workerThread.Start();

            Wait.While(() => !workerThread.Thread.IsAlive, 300);

            manualResetEvent.WaitOne(500);
            workerThread.Stop();

            Wait.While(() => workerThread.Thread.IsAlive, 300);

            Assert.AreEqual(false, workerThread.Thread.IsAlive);

            Assert.AreEqual(true, workerThreadExitingEventIsCalled);

            if (otherThreadsException != null)
            {
                throw otherThreadsException;
            }
        }
开发者ID:QuickOrBeDead,项目名称:Labo.Threading,代码行数:61,代码来源:WorkerThreadFixture.cs

示例7: DoWorkShouldCallWorkerThreadWorkItemExceptionEventWhenWorkItemIsCompletedWithException

        public void DoWorkShouldCallWorkerThreadWorkItemExceptionEventWhenWorkItemIsCompletedWithException()
        {
            WorkerThreadManagerStub workerThreadManager = WorkerThreadManagerStub.Create(); 

            Exception invalidCastException = new InvalidCastException();
            ManualResetEvent manualResetEvent = new ManualResetEvent(false);
            IWorkItemQueue workItemQueue = Substitute.For<IWorkItemQueue>();
            IWorkItem workItem = Substitute.For<IWorkItem>();
            workItem.When(x => x.DoWork()).Do(
                x =>
                    {
                        throw invalidCastException;
                    });
            workItemQueue.Dequeue().Returns(workItem);

            Exception otherThreadsException = null;
            bool workerThreadWorkItemExceptionEventIsCalled = false;
            WorkerThread workerThread = new WorkerThread(workerThreadManager, workItemQueue, "Test", false);
            workerThreadManager.WorkerThreadWorkItemException += (sender, args) =>
                {
                    workerThreadWorkItemExceptionEventIsCalled = true;

                    try
                    {
                        Assert.AreEqual(WorkItemState.CompletedWithException, workItem.State);
                        Assert.AreEqual(workItem, args.WorkItem);
                        Assert.AreEqual(workItem.LastException, args.Exception);
                        Assert.AreEqual(invalidCastException, args.Exception);
                        workItem.Received(1).DoWork();
                    }
                    catch (Exception ex)
                    {
                        otherThreadsException = ex;
                    }

                    workerThread.Stop();
                    manualResetEvent.Set();

                };
            workerThread.Start();

            manualResetEvent.WaitOne(500);
            workerThread.Stop();

            Wait.While(() => workerThread.Thread.IsAlive, 300);

            Assert.AreEqual(false, workerThread.Thread.IsAlive);

            Assert.AreEqual(true, workerThreadWorkItemExceptionEventIsCalled);

            if (otherThreadsException != null)
            {
                throw otherThreadsException;
            }
        }
开发者ID:QuickOrBeDead,项目名称:Labo.Threading,代码行数:55,代码来源:WorkerThreadFixture.cs

示例8: DoWorkShouldCallWorkerThreadWorkItemStartingBeforeProcessingWorkItem

        public void DoWorkShouldCallWorkerThreadWorkItemStartingBeforeProcessingWorkItem()
        {
            WorkerThreadManagerStub workerThreadManager = WorkerThreadManagerStub.Create(); 

            ManualResetEvent manualResetEvent = new ManualResetEvent(false);
            IWorkItemQueue workItemQueue = Substitute.For<IWorkItemQueue>();
            IWorkItem workItem = Substitute.For<IWorkItem>();
            workItemQueue.Dequeue().Returns(workItem);

            Exception otherThreadsException = null;
            WorkerThread workerThread = new WorkerThread(workerThreadManager, workItemQueue, "Test", false);
            workerThreadManager.WorkerThreadWorkItemStarting += (sender, args) =>
            {
                try
                {
                    Assert.AreEqual(workItem, args.WorkItem);

                    workItem.DidNotReceive().DoWork();
                }
                catch (Exception ex)
                {
                    otherThreadsException = ex;
                }
               
                workerThread.Stop();
                manualResetEvent.Set();
            };
            workerThread.Start();

            manualResetEvent.WaitOne(500);
            workerThread.Stop();

            Wait.While(() => workerThread.Thread.IsAlive, 300);

            Assert.AreEqual(false, workerThread.Thread.IsAlive);

            if (otherThreadsException != null)
            {
                throw otherThreadsException;
            }
        }
开发者ID:QuickOrBeDead,项目名称:Labo.Threading,代码行数:41,代码来源:WorkerThreadFixture.cs

示例9: DoWorkShouldExitWorkerThreadWhenStopWhenWorkItemQueueIsEmptyIsTrueAndWorkItemQueueIsEmpty

        public void DoWorkShouldExitWorkerThreadWhenStopWhenWorkItemQueueIsEmptyIsTrueAndWorkItemQueueIsEmpty()
        {
            WorkerThreadManagerStub workerThreadManager = WorkerThreadManagerStub.Create(); 

            ManualResetEvent manualResetEvent = new ManualResetEvent(false);
            IWorkItemQueue workItemQueue = Substitute.For<IWorkItemQueue>();
            workItemQueue.Dequeue().Returns((IWorkItem)null);

            bool exitedBecaseStopWhenWorkItemQueueIsEmptyFlagIsTrue = false;
            WorkerThread workerThread = new WorkerThread(workerThreadManager, workItemQueue, "Test", false);
            workerThreadManager.WorkerThreadExiting += (sender, args) =>
            {
                if (args.WorkerThreadExitReason == WorkerThreadExitReason.StopWhenWorkItemQueueIsEmptyIsTrue)
                {
                    exitedBecaseStopWhenWorkItemQueueIsEmptyFlagIsTrue = true;
                    manualResetEvent.Set();
                }
            };
            workerThreadManager.ShouldExitWorkerThread(workerThread, false).Returns(false);
            workerThreadManager.ShouldExitWorkerThread(workerThread, true).Returns(false);

            workerThread.StopWhenWorkItemQueueIsEmpty(true);
            workerThread.Start();

            manualResetEvent.WaitOne(500);
            workerThread.Stop();

            Wait.While(() => workerThread.Thread.IsAlive, 300);

            Assert.AreEqual(false, workerThread.Thread.IsAlive);

            workItemQueue.Received(1).Dequeue();
            Assert.AreEqual(true, exitedBecaseStopWhenWorkItemQueueIsEmptyFlagIsTrue);
        }
开发者ID:QuickOrBeDead,项目名称:Labo.Threading,代码行数:34,代码来源:WorkerThreadFixture.cs


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