本文整理汇总了C#中Task.Catch方法的典型用法代码示例。如果您正苦于以下问题:C# Task.Catch方法的具体用法?C# Task.Catch怎么用?C# Task.Catch使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Task
的用法示例。
在下文中一共展示了Task.Catch方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Catch_WithInputValue_IncompleteTaskOfSuccess_DoesNotRunContinuationAndDoesNotSwitchContexts
public Task Catch_WithInputValue_IncompleteTaskOfSuccess_DoesNotRunContinuationAndDoesNotSwitchContexts() {
// Arrange
bool ranContinuation = false;
var syncContext = new Mock<SynchronizationContext> { CallBase = true };
SynchronizationContext.SetSynchronizationContext(syncContext.Object);
Task<int> incompleteTask = new Task<int>(() => 42);
// Act
Task<int> resultTask = incompleteTask.Catch(info => {
ranContinuation = true;
return info.Handled(42);
});
// Assert
incompleteTask.Start();
return resultTask.ContinueWith(task => {
Assert.False(ranContinuation);
syncContext.Verify(sc => sc.Post(It.IsAny<SendOrPostCallback>(), null), Times.Never());
});
}
示例2: Catch_WithInputValue_IncompleteTaskOfFault_RunsOnNewThreadAndPostsToSynchronizationContext
public Task Catch_WithInputValue_IncompleteTaskOfFault_RunsOnNewThreadAndPostsToSynchronizationContext() {
// Arrange
int outerThreadId = Thread.CurrentThread.ManagedThreadId;
int innerThreadId = Int32.MinValue;
Exception thrownException = new Exception();
Exception caughtException = null;
var syncContext = new Mock<SynchronizationContext> { CallBase = true };
SynchronizationContext.SetSynchronizationContext(syncContext.Object);
Task<int> incompleteTask = new Task<int>(() => { throw thrownException; });
// Act
Task<int> resultTask = incompleteTask.Catch(info => {
caughtException = info.Exception;
innerThreadId = Thread.CurrentThread.ManagedThreadId;
return info.Handled(42);
});
// Assert
incompleteTask.Start();
return resultTask.ContinueWith(task => {
Assert.Same(thrownException, caughtException);
Assert.NotEqual(innerThreadId, outerThreadId);
syncContext.Verify(sc => sc.Post(It.IsAny<SendOrPostCallback>(), null), Times.Once());
});
}
示例3: ReadAsync
private void ReadAsync(Task<int> readTask)
{
readTask.Catch(ex => Close(ex))
.Then(read =>
{
if (TryProcessRead(read))
{
Process();
}
})
.Catch();
}