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


C# WorkerThread.Start方法代码示例

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


在下文中一共展示了WorkerThread.Start方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: Start

 /// <summary>
 /// Blocks until all the ReceivingThreads are started.
 /// </summary>
 public override void Start()
 {
     Logger.Info("Starting service '" + ServiceName + "'.");
     Logger.Info("Configuration: " + this.PrettyFormat());
     for (int i = 0; i < NumberOfWorkingThreads; i++)
     {
         WorkerThread t = new WorkerThread("Receiving thread " + i.ToString("00"), ReceiveOneMessageFromTransport);
         t.Stopped += (x, e) =>
         {
             if (e.Error != null)
             {
                 WorkerThread worker = (WorkerThread)x;
                 Logger.Error(worker.Id + " threw an exception", e.Error);
             }
         };
         ReceivingThreads.Add(t);
         t.Start();
     }
 }
开发者ID:rodolfograve,项目名称:Romialyo.NET-SDK,代码行数:22,代码来源:MessagesProcessorServiceBase.cs

示例3: 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

示例4: RunInThread

        /// <summary>
        /// Run the given <see cref="IThreadRunnable" /> object in the next available
        /// <see cref="Thread" />. If while waiting the thread pool is asked to
        /// shut down, the Runnable is executed immediately within a new additional
        /// thread.
        /// </summary>
        /// <param name="runnable">The <see cref="IThreadRunnable" /> to be added.</param>
        public virtual bool RunInThread(IThreadRunnable runnable)
        {
            if (runnable == null)
            {
                return false;
            }

            lock (nextRunnableLock)
            {
                handoffPending = true;

                // Wait until a worker thread is available
                while ((availWorkers.Count < 1) && !isShutdown)
                {
                    try
                    {
                        Monitor.Wait(nextRunnableLock, 500);
                    }
                    catch (ThreadInterruptedException)
                    {
                    }
                }

                if (!isShutdown)
                {
                    WorkerThread wt = (WorkerThread)availWorkers[0];
                    availWorkers.RemoveAt(0);
                    busyWorkers.Add(wt);
                    wt.Run(runnable);
                }
                else
                {
                    // If the thread pool is going down, execute the Runnable
                    // within a new additional worker thread (no thread from the pool).
                    WorkerThread wt =
                        new WorkerThread(this, "WorkerThread-LastJob", prio, MakeThreadsDaemons, runnable);
                    busyWorkers.Add(wt);
                    workers.Add(wt);
                    wt.Start();
                }
                Monitor.PulseAll(nextRunnableLock);
                handoffPending = false;
            }

            return true;
        }
开发者ID:kisflying,项目名称:kion,代码行数:53,代码来源:SimpleThreadPool.cs

示例5: 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

示例6: 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

示例7: 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

示例8: 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

示例9: 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

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