当前位置: 首页>>代码示例>>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;未经允许,请勿转载。