本文整理匯總了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;
});
}
示例2: TryCompleteFromCompletedTask_PropagatesCancellation
public void TryCompleteFromCompletedTask_PropagatesCancellation()
{
AsyncContext.Run(async () =>
{
var tcs = new TaskCompletionSource();
tcs.TryCompleteFromCompletedTask(TaskConstants.Canceled);
await AssertEx.ThrowsExceptionAsync<OperationCanceledException>(() => tcs.Task);
});
}
示例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);
});
}
示例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);
});
}
示例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);
});
}
示例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);
});
}
示例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;
}
示例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;
}
}
示例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();
}
示例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);
});
}