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