本文整理汇总了C#中System.Threading.Task.Then方法的典型用法代码示例。如果您正苦于以下问题:C# Task.Then方法的具体用法?C# Task.Then怎么用?C# Task.Then使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Threading.Task
的用法示例。
在下文中一共展示了Task.Then方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Add
public void Add(Task task)
{
if (_tasks.Count >= _poolSize)
{
var tasks = _tasks.Keys.ToArray();
try
{
Task.WaitAny(tasks);
}
catch (Exception ex)
{
Fail(ex);
return;
}
}
_tasks.TryAdd(task, true);
task.Then(() =>
{
RemoveTask(task);
}).Catch(ex =>
{
RemoveTask(task);
Fail(ex);
});
}
示例2: Task
public Task Then_NoInputValue_WithReturnValue_IncompleteTask_RunsOnNewThreadAndPostsContinuationToSynchronizationContext() {
// Arrange
int originalThreadId = Thread.CurrentThread.ManagedThreadId;
int callbackThreadId = Int32.MinValue;
var syncContext = new Mock<SynchronizationContext> { CallBase = true };
SynchronizationContext.SetSynchronizationContext(syncContext.Object);
Task incompleteTask = new Task(() => { });
// Act
Task resultTask = incompleteTask.Then(() => {
callbackThreadId = Thread.CurrentThread.ManagedThreadId;
return 42;
});
// Assert
incompleteTask.Start();
return resultTask.ContinueWith(task => {
Assert.NotEqual(originalThreadId, callbackThreadId);
syncContext.Verify(sc => sc.Post(It.IsAny<SendOrPostCallback>(), null), Times.Once());
});
}
示例3: OnTransactionCompleting
private void OnTransactionCompleting(RedisTransaction transaction, Task<bool> transactionTask, IList<Message> messages, TaskCompletionSource<object> tcs)
{
if (transactionTask.IsCompleted)
{
try
{
OnTransactionComplete(transaction, transactionTask.Result, messages, tcs);
}
catch (OperationCanceledException)
{
tcs.TrySetCanceled();
}
catch (Exception ex)
{
tcs.TrySetException(ex);
}
}
else
{
transactionTask.Then(result => OnTransactionComplete(transaction, result, messages, tcs))
.ContinueWithNotComplete(tcs);
}
}