本文整理汇总了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();
}
示例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);
}
示例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);
}
示例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();
}
示例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();
}
示例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;
}
}
示例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;
}
}
示例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;
}
}
示例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);
}