本文整理汇总了C#中System.Threading.Tasks.Task.Then方法的典型用法代码示例。如果您正苦于以下问题:C# Task.Then方法的具体用法?C# Task.Then怎么用?C# Task.Then使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Threading.Tasks.Task
的用法示例。
在下文中一共展示了Task.Then方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SerializeAsync
public Task SerializeAsync(Task<HttpResponseMessage> response, Stream stream)
{
return response.Then(r =>
{
if (r.Content != null)
{
return r.Content.LoadIntoBufferAsync()
.Then(() =>
{
var httpMessageContent = new HttpMessageContent(r);
// All in-memory and CPU-bound so no need to async
return httpMessageContent.ReadAsByteArrayAsync();
})
.Then(buffer =>
{
return Task.Factory.FromAsync(stream.BeginWrite, stream.EndWrite,
buffer, 0, buffer.Length, null, TaskCreationOptions.AttachedToParent);
}
);
;
}
else
{
var httpMessageContent = new HttpMessageContent(r);
// All in-memory and CPU-bound so no need to async
var buffer = httpMessageContent.ReadAsByteArrayAsync().Result;
return Task.Factory.FromAsync(stream.BeginWrite, stream.EndWrite,
buffer, 0, buffer.Length, null, TaskCreationOptions.AttachedToParent);
}
}
);
}
示例2: ThenTest_ThrowEx
public void ThenTest_ThrowEx()
{
bool a = false, b = false;
var tInitial = new Task(() => { a = true; throw new Exception("ex A"); });
var tThen = tInitial.Then(() => { b = true; });
try
{
tInitial.Start();
tThen.Wait();
Assert.Fail();
}
catch (AggregateException ex)
{
Assert.AreEqual(1, ex.InnerExceptions.Count);
Assert.AreEqual("ex A", ex.InnerExceptions[0].Message);
Assert.IsTrue(a);
Assert.IsFalse(b);
}
}
示例3: Then_WithInputValue_WithTaskReturnValue_IncompleteTask_HandlesContinuationException
public Task Then_WithInputValue_WithTaskReturnValue_IncompleteTask_HandlesContinuationException()
{
// Arrange
Task<int> incompleteTask = new Task<int>(() => 21);
// Act
Task resultTask = incompleteTask.Then(result =>
{
throw _expectedCatchInfoException;
}).Catch(info =>
{
Assert.Same(_expectedCatchInfoException, info.Exception);
return info.Handled();
});
// Assert
incompleteTask.Start();
return resultTask;
}
示例4: Then_WithInputValue_WithTaskReturnValue_IncompleteTask_RunsOnNewThreadAndPostsContinuationExceptionToSynchronizationContext
public Task Then_WithInputValue_WithTaskReturnValue_IncompleteTask_RunsOnNewThreadAndPostsContinuationExceptionToSynchronizationContext()
{
// Arrange
int originalThreadId = Thread.CurrentThread.ManagedThreadId;
int callbackThreadId = Int32.MinValue;
var syncContext = new Mock<SynchronizationContext> { CallBase = true };
SynchronizationContext.SetSynchronizationContext(syncContext.Object);
Task<int> incompleteTask = new Task<int>(() => 21);
// Act
Task resultTask = incompleteTask.Then(result =>
{
callbackThreadId = Thread.CurrentThread.ManagedThreadId;
throw _expectedCatchInfoException;
}).Catch(info =>
{
Assert.NotEqual(originalThreadId, callbackThreadId);
Assert.Same(_expectedCatchInfoException, info.Exception);
return info.Handled();
});
// Assert
incompleteTask.Start();
return resultTask.ContinueWith(task =>
{
Assert.NotEqual(originalThreadId, callbackThreadId);
syncContext.Verify(sc => sc.Post(It.IsAny<SendOrPostCallback>(), null), Times.Exactly(2));
});
}
示例5: Then_WithInputValue_WithTaskReturnValue_IncompleteTask_RunsOnNewThreadAndPostsContinuationToSynchronizationContext
public Task Then_WithInputValue_WithTaskReturnValue_IncompleteTask_RunsOnNewThreadAndPostsContinuationToSynchronizationContext()
{
// Arrange
int originalThreadId = Thread.CurrentThread.ManagedThreadId;
int callbackThreadId = Int32.MinValue;
var syncContext = new Mock<SynchronizationContext> { CallBase = true };
SynchronizationContext.SetSynchronizationContext(syncContext.Object);
Task<int> incompleteTask = new Task<int>(() => 21);
// Act
Task resultTask = incompleteTask.Then(result =>
{
callbackThreadId = Thread.CurrentThread.ManagedThreadId;
return TaskHelpers.FromResult(42);
});
// Assert
incompleteTask.Start();
return resultTask.ContinueWith(task =>
{
Assert.NotEqual(originalThreadId, callbackThreadId);
syncContext.Verify(sc => sc.Post(It.IsAny<SendOrPostCallback>(), null), Times.Once());
});
}
示例6: Then_NoInputValue_NoReturnValue_IncompleteTask_HandlesContinuationException
public Task Then_NoInputValue_NoReturnValue_IncompleteTask_HandlesContinuationException()
{
// Arrange
Task incompleteTask = new Task(() => { });
// Act
Task resultTask = incompleteTask.Then(() =>
{
throw _expectedCatchInfoException;
}).Catch(catchinfo =>
{
Assert.Same(_expectedCatchInfoException, catchinfo.Exception);
return catchinfo.Handled();
});
// Assert
incompleteTask.Start();
return resultTask;
}
示例7: Commit
public static Task<long> Commit(this IStorageNode Node, Task<long> TransactionID, Dictionary<string, string> Updated, string[] Read)
{
return TransactionID
.Then((WriteID) => Node.Commit(WriteID, Updated, Read))
.Unwrap();
}
示例8: Read
public static Task<string> Read(this IStorageNode Node, Task<long> TransactionID, string Key)
{
return TransactionID
.Then((ReadID) => Node.Read(ReadID, Key))
.Unwrap();
}