本文整理汇总了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);
});
}