當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。