本文整理汇总了C#中JobHost.DoWork方法的典型用法代码示例。如果您正苦于以下问题:C# JobHost.DoWork方法的具体用法?C# JobHost.DoWork怎么用?C# JobHost.DoWork使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类JobHost
的用法示例。
在下文中一共展示了JobHost.DoWork方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ThrowsArgumentNullExceptionIfWorkIsNull
public void ThrowsArgumentNullExceptionIfWorkIsNull()
{
var host = new JobHost();
var exception = Assert.Throws<ArgumentNullException>(() => host.DoWork(null));
Assert.Equal("work", exception.ParamName);
}
示例2: EnsuresNoWorkIsDone
public void EnsuresNoWorkIsDone()
{
var host = new JobHost();
var task = new Task(() => { throw new InvalidOperationException("Hey, this is supposed to be shut down!"); });
host.Stop(true);
host.DoWork(task);
}
示例3: DoesNotCallStartIfWorkIsAlreadyScheduledOrCompleted
public void DoesNotCallStartIfWorkIsAlreadyScheduledOrCompleted()
{
var tcs = new TaskCompletionSource<object>();
tcs.SetResult(null);
var task = tcs.Task;
var host = new JobHost();
Assert.DoesNotThrow(() => host.DoWork(task));
}
示例4: WaitsForTaskToComplete
public void WaitsForTaskToComplete()
{
var host = new JobHost();
var workTask = new Task(() => host.DoWork(new Task(() =>
{
// Was getting inconsistent results with Thread.Sleep(100)
for (int i = 0; i < 100; i++)
{
Thread.Sleep(1);
}
})));
var beforeStop = DateTime.UtcNow;
workTask.Start();
while (workTask.Status != TaskStatus.Running)
{
Thread.Sleep(1);
}
host.Stop(false);
var afterStop = DateTime.UtcNow;
// If Stop didn't wait, we'd expect after to be less than 100 ms larger than beforeStop
Assert.True((afterStop - beforeStop).TotalMilliseconds >= 100);
}