当前位置: 首页>>代码示例>>C#>>正文


C# Task.Then方法代码示例

本文整理汇总了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);
				}
			}
				);
		}
开发者ID:CoryAlice93,项目名称:WebAPIContrib,代码行数:34,代码来源:MessageContentHttpMessageSerializer.cs

示例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);
      }
    }
开发者ID:sergey-msu,项目名称:nfx,代码行数:20,代码来源:TaskUtilsTest.cs

示例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;
        }
开发者ID:naulizzang,项目名称:aspnetwebstack,代码行数:20,代码来源:TaskHelpersExtensionsTest.cs

示例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));
            });
        }
开发者ID:naulizzang,项目名称:aspnetwebstack,代码行数:31,代码来源:TaskHelpersExtensionsTest.cs

示例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());
            });
        }
开发者ID:naulizzang,项目名称:aspnetwebstack,代码行数:26,代码来源:TaskHelpersExtensionsTest.cs

示例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;
        }
开发者ID:naulizzang,项目名称:aspnetwebstack,代码行数:20,代码来源:TaskHelpersExtensionsTest.cs

示例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();
 }
开发者ID:ForbesLindesay,项目名称:wrench,代码行数:6,代码来源:StorageConnectionExtensions.cs

示例8: Read

 public static Task<string> Read(this IStorageNode Node, Task<long> TransactionID, string Key)
 {
     return TransactionID
         .Then((ReadID) => Node.Read(ReadID, Key))
         .Unwrap();
 }
开发者ID:ForbesLindesay,项目名称:wrench,代码行数:6,代码来源:StorageConnectionExtensions.cs


注:本文中的System.Threading.Tasks.Task.Then方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。