當前位置: 首頁>>代碼示例>>C#>>正文


C# TaskCompletionSource.TryCompleteFromCompletedTask方法代碼示例

本文整理匯總了C#中Nito.AsyncEx.TaskCompletionSource.TryCompleteFromCompletedTask方法的典型用法代碼示例。如果您正苦於以下問題:C# TaskCompletionSource.TryCompleteFromCompletedTask方法的具體用法?C# TaskCompletionSource.TryCompleteFromCompletedTask怎麽用?C# TaskCompletionSource.TryCompleteFromCompletedTask使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Nito.AsyncEx.TaskCompletionSource的用法示例。


在下文中一共展示了TaskCompletionSource.TryCompleteFromCompletedTask方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: TryCompleteFromCompletedTask_PropagatesResult

 public void TryCompleteFromCompletedTask_PropagatesResult()
 {
     AsyncContext.Run(async () =>
     {
         var tcs = new TaskCompletionSource();
         tcs.TryCompleteFromCompletedTask(TaskConstants.Completed);
         await tcs.Task;
     });
 }
開發者ID:Lakerfield,項目名稱:AsyncEx,代碼行數:9,代碼來源:TaskCompletionSourceExtensionsUnitTests.cs

示例2: TryCompleteFromCompletedTask_PropagatesCancellation

 public void TryCompleteFromCompletedTask_PropagatesCancellation()
 {
     AsyncContext.Run(async () =>
     {
         var tcs = new TaskCompletionSource();
         tcs.TryCompleteFromCompletedTask(TaskConstants.Canceled);
         await AssertEx.ThrowsExceptionAsync<OperationCanceledException>(() => tcs.Task);
     });
 }
開發者ID:Lakerfield,項目名稱:AsyncEx,代碼行數:9,代碼來源:TaskCompletionSourceExtensionsUnitTests.cs

示例3: TryCompleteFromCompletedTaskTResult_PropagatesCancellation

 public void TryCompleteFromCompletedTaskTResult_PropagatesCancellation()
 {
     Test.Async(async () =>
     {
         var tcs = new TaskCompletionSource<int>();
         tcs.TryCompleteFromCompletedTask(TaskConstants<int>.Canceled);
         await AssertEx.ThrowsExceptionAsync<OperationCanceledException>(() => tcs.Task);
     });
 }
開發者ID:Lakerfield,項目名稱:AsyncEx,代碼行數:9,代碼來源:TaskCompletionSourceExtensionsUnitTests.cs

示例4: TryCompleteFromCompletedTask_WithDifferentTResult_PropagatesResult

 public void TryCompleteFromCompletedTask_WithDifferentTResult_PropagatesResult()
 {
     AsyncContext.Run(async () =>
     {
         var tcs = new TaskCompletionSource<object>();
         tcs.TryCompleteFromCompletedTask(TaskConstants.Int32NegativeOne);
         var result = await tcs.Task;
         Assert.AreEqual(-1, result);
     });
 }
開發者ID:Lakerfield,項目名稱:AsyncEx,代碼行數:10,代碼來源:TaskCompletionSourceExtensionsUnitTests.cs

示例5: TryCompleteFromCompletedTaskTResult_PropagatesResult

 public void TryCompleteFromCompletedTaskTResult_PropagatesResult()
 {
     Test.Async(async () =>
     {
         var tcs = new TaskCompletionSource<int>();
         tcs.TryCompleteFromCompletedTask(TaskConstants.Int32NegativeOne);
         var result = await tcs.Task;
         Assert.AreEqual(-1, result);
     });
 }
開發者ID:Lakerfield,項目名稱:AsyncEx,代碼行數:10,代碼來源:TaskCompletionSourceExtensionsUnitTests.cs

示例6: TryCompleteFromCompletedTaskTResult_PropagatesException

        public void TryCompleteFromCompletedTaskTResult_PropagatesException()
        {
            AsyncContext.Run(async () =>
            {
                var source = new TaskCompletionSource<int>();
                source.TrySetException(new NotImplementedException());

                var tcs = new TaskCompletionSource<int>();
                tcs.TryCompleteFromCompletedTask(source.Task);
                await AssertEx.ThrowsExceptionAsync<NotImplementedException>(() => tcs.Task);
            });
        }
開發者ID:Lakerfield,項目名稱:AsyncEx,代碼行數:12,代碼來源:TaskCompletionSourceExtensionsUnitTests.cs

示例7: ToBegin

        /// <summary>
        /// Wraps a <see cref="Task"/> into the Begin method of an APM pattern.
        /// </summary>
        /// <param name="task">The task to wrap.</param>
        /// <param name="callback">The callback method passed into the Begin method of the APM pattern.</param>
        /// <param name="state">The state passed into the Begin method of the APM pattern.</param>
        /// <returns>The asynchronous operation, to be returned by the Begin method of the APM pattern.</returns>
        public static IAsyncResult ToBegin(Task task, AsyncCallback callback, object state)
        {
            var tcs = new TaskCompletionSource(state);
            task.ContinueWith(t =>
            {
                tcs.TryCompleteFromCompletedTask(t);

                if (callback != null)
                    callback(tcs.Task);
            }, TaskScheduler.Default);

            return tcs.Task;
        }
開發者ID:Lakerfield,項目名稱:AsyncEx,代碼行數:20,代碼來源:AsyncFactory.cs

示例8: WaitAsync

        /// <summary>
        /// Asynchronously waits for a signal on this condition variable. The associated lock MUST be held when calling this method, and it will still be held when this method returns, even if the method is cancelled.
        /// </summary>
        /// <param name="cancellationToken">The cancellation signal used to cancel this wait.</param>
        public Task WaitAsync(CancellationToken cancellationToken)
        {
            lock (_mutex)
            {
                // Begin waiting for either a signal or cancellation.
                var task = _queue.Enqueue(_mutex, cancellationToken);

                // Attach to the signal or cancellation.
                var retTcs = new TaskCompletionSource();
                task.ContinueWith(async t =>
                {
                    // Re-take the lock.
                    await _asyncLock.LockAsync().ConfigureAwait(false);

                    // Propagate the cancellation exception if necessary.
                    retTcs.TryCompleteFromCompletedTask(t);
                }, CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default);

                var ret = retTcs.Task;
                //Enlightenment.Trace.AsyncConditionVariable_TrackWait(this, _asyncLock, task, ret);

                // Release the lock while we are waiting.
                _asyncLock.ReleaseLock();

                return ret;
            }
        }
開發者ID:Lakerfield,項目名稱:AsyncEx,代碼行數:31,代碼來源:AsyncConditionVariable.cs

示例9: Upgrade

            /// <summary>
            /// Synchronously upgrades the reader lock to a writer lock. Returns a disposable that downgrades the writer lock to a reader lock when disposed. This method may block the calling thread.
            /// </summary>
            /// <param name="cancellationToken">The cancellation token used to cancel the upgrade. If this is already set, then this method will attempt to upgrade immediately (succeeding if the lock is currently available).</param>
            public IDisposable Upgrade(CancellationToken cancellationToken)
            {
                lock (_asyncReaderWriterLock.SyncObject)
                {
                    if (_upgrade != null)
                        throw new InvalidOperationException("Cannot upgrade.");

                    _upgrade = _asyncReaderWriterLock.UpgradeAsync(cancellationToken);
                }

                _asyncReaderWriterLock.ReleaseWaitersWhenCanceled(_upgrade);
                var ret = new TaskCompletionSource<IDisposable>();
                _upgrade.ContinueWith(t =>
                {
                    if (t.IsCanceled)
                        lock (_asyncReaderWriterLock.SyncObject) { _upgrade = null; }
                    ret.TryCompleteFromCompletedTask(t);
                }, CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default);
                return ret.Task.WaitAndUnwrapException();
            }
開發者ID:Lakerfield,項目名稱:AsyncEx,代碼行數:24,代碼來源:AsyncReaderWriterLock.cs

示例10: TryCompleteFromCompletedTask_PropagatesException

        public void TryCompleteFromCompletedTask_PropagatesException()
        {
            Test.Async(async () =>
            {
                var source = new TaskCompletionSource();
                source.TrySetException(new NotImplementedException());

                var tcs = new TaskCompletionSource();
                tcs.TryCompleteFromCompletedTask(source.Task);
                await AssertEx.ThrowsExceptionAsync<NotImplementedException>(() => tcs.Task);
            });
        }
開發者ID:Lakerfield,項目名稱:AsyncEx,代碼行數:12,代碼來源:TaskCompletionSourceExtensionsUnitTests.cs


注:本文中的Nito.AsyncEx.TaskCompletionSource.TryCompleteFromCompletedTask方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。