本文整理匯總了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);
}
}