本文整理汇总了C#中Nito.AsyncEx.TaskCompletionSource类的典型用法代码示例。如果您正苦于以下问题:C# TaskCompletionSource类的具体用法?C# TaskCompletionSource怎么用?C# TaskCompletionSource使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
TaskCompletionSource类属于Nito.AsyncEx命名空间,在下文中一共展示了TaskCompletionSource类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AsyncLock_Locked_PreventsLockUntilUnlocked
public void AsyncLock_Locked_PreventsLockUntilUnlocked()
{
AsyncContext.Run(async () =>
{
var mutex = new AsyncLock();
var task1HasLock = new TaskCompletionSource();
var task1Continue = new TaskCompletionSource();
Task<IDisposable> task1LockTask = null;
var task1 = Task.Run(async () =>
{
task1LockTask = mutex.LockAsync();
using (await task1LockTask)
{
task1HasLock.SetResult();
await task1Continue.Task;
}
});
await task1HasLock.Task;
Task<IDisposable> task2LockTask = null;
var task2Start = Task.Factory.StartNew(async () =>
{
task2LockTask = mutex.LockAsync();
await task2LockTask;
});
var task2 = await task2Start;
Assert.IsFalse(task2.IsCompleted);
task1Continue.SetResult();
await task2;
});
}
示例2: AsyncLock_Locked_PreventsLockUntilUnlocked
public void AsyncLock_Locked_PreventsLockUntilUnlocked()
{
Test.Async(async () =>
{
var mutex = new AsyncLock();
var task1HasLock = new TaskCompletionSource();
var task1Continue = new TaskCompletionSource();
var task1 = TaskShim.Run(async () =>
{
using (await mutex.LockAsync())
{
task1HasLock.SetResult();
await task1Continue.Task;
}
});
await task1HasLock.Task;
var task2Start = Task.Factory.StartNew(async () =>
{
await mutex.LockAsync();
});
var task2 = await task2Start;
Assert.IsFalse(task2.IsCompleted);
task1Continue.SetResult();
await task2;
});
}
示例3: ProgressReport_NotifiesChangeOnCapturedSynchronizationContext
public void ProgressReport_NotifiesChangeOnCapturedSynchronizationContext()
{
Test.Async(async () =>
{
SynchronizationContext updateContext = null;
SynchronizationContext threadContext = null;
var tcs = new TaskCompletionSource();
using (var thread = new AsyncContextThread())
{
threadContext = await thread.Factory.Run(() => SynchronizationContext.Current);
PropertyProgress<int> propertyProgress = await thread.Factory.Run(() => new PropertyProgress<int>());
propertyProgress.PropertyChanged += (_, e) =>
{
updateContext = SynchronizationContext.Current;
tcs.SetResult();
};
IProgress<int> progress = propertyProgress;
progress.Report(13);
await tcs.Task;
}
Assert.IsNotNull(updateContext);
Assert.AreEqual(threadContext, updateContext);
});
}
示例4: InvokeAsync
public static Task InvokeAsync([NotNull] this IDispatcherService service, [NotNull] Func<Task> action)
{
Guard.NotNull(service, nameof(service));
Guard.NotNull(action, nameof(action));
var tcs = new TaskCompletionSource();
service.InvokeAsync(new Action(() =>
{
action().ContinueWith(t =>
{
if (t.Exception != null)
{
tcs.TrySetException(t.Exception);
return;
}
if (t.IsCanceled)
{
tcs.TrySetCanceled();
}
tcs.TrySetResult();
});
}));
return tcs.Task;
}
示例5: AsyncBarrier
/// <summary>
/// Creates an async-compatible barrier.
/// </summary>
/// <param name="participants">The number of participants.</param>
public AsyncBarrier(int participants)
{
_sync = new object();
_tcs = new TaskCompletionSource();
_participants = _count = participants;
//Enlightenment.Trace.AsyncBarrier_PhaseChanged(this, 0, participants, _tcs.Task);
}
示例6: Locked_PreventsLockUntilUnlocked
public void Locked_PreventsLockUntilUnlocked()
{
AsyncContext.Run(async () =>
{
var monitor = new AsyncMonitor();
var task1HasLock = new TaskCompletionSource();
var task1Continue = new TaskCompletionSource();
Task<IDisposable> initialLockTask = null;
var task1 = Task.Run(async () =>
{
initialLockTask = monitor.EnterAsync();
using (await initialLockTask)
{
task1HasLock.SetResult();
await task1Continue.Task;
}
});
await task1HasLock.Task;
var lockTask = monitor.EnterAsync();
Assert.IsFalse(lockTask.IsCompleted);
task1Continue.SetResult();
await lockTask;
});
}
示例7: TrySetException_FaultsTask
public void TrySetException_FaultsTask()
{
var e = new InvalidOperationException();
var tcs = new TaskCompletionSource();
tcs.TrySetException(e);
Assert.IsTrue(tcs.Task.IsFaulted);
Assert.AreSame(e, tcs.Task.Exception.InnerException);
}
示例8: ConstructorWithStateAndOptions_SetsAsyncStateAndOptions
public void ConstructorWithStateAndOptions_SetsAsyncStateAndOptions()
{
var state = new object();
var options = TaskCreationOptions.AttachedToParent;
var tcs = new TaskCompletionSource(state, options);
Assert.AreSame(state, tcs.Task.AsyncState);
Assert.AreEqual(options, tcs.Task.CreationOptions);
}
示例9: SetExceptions_FaultsTask
public void SetExceptions_FaultsTask()
{
var e = new[] { new InvalidOperationException() };
var tcs = new TaskCompletionSource();
tcs.SetException(e);
Assert.IsTrue(tcs.Task.IsFaulted);
Assert.IsTrue(tcs.Task.Exception.InnerExceptions.SequenceEqual(e));
}
示例10: GetSomeTextAsync
private static Task<string> GetSomeTextAsync()
{
// https://msdn.microsoft.com/en-us/library/hh873177(v=vs.110).aspx
var tcs = new TaskCompletionSource<string>();
tcs.SetResult("some text");
return tcs.Task;
}
示例11: AsyncManualResetEvent
/// <summary>
/// Creates an async-compatible manual-reset event.
/// </summary>
/// <param name="set">Whether the manual-reset event is initially set or unset.</param>
public AsyncManualResetEvent(bool set) {
_sync = new object();
_tcs = new TaskCompletionSource();
if (set) {
//Enlightenment.Trace.AsyncManualResetEvent_Set(this, _tcs.Task);
_tcs.SetResult();
}
}
示例12: SignalAndWaitAsync_Underflow_ThrowsException
public void SignalAndWaitAsync_Underflow_ThrowsException()
{
var tcs = new TaskCompletionSource();
var barrier = new AsyncBarrier(1, async _ => { await tcs.Task; });
barrier.SignalAndWaitAsync();
AssertEx.ThrowsException<InvalidOperationException>(() => barrier.SignalAndWaitAsync());
tcs.SetResult();
}
示例13: TryCompleteFromCompletedTask_PropagatesCancellation
public void TryCompleteFromCompletedTask_PropagatesCancellation()
{
AsyncContext.Run(async () =>
{
var tcs = new TaskCompletionSource();
tcs.TryCompleteFromCompletedTask(TaskConstants.Canceled);
await AssertEx.ThrowsExceptionAsync<OperationCanceledException>(() => tcs.Task);
});
}
示例14: 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);
});
}
示例15: FromTask_ReturnsTokenWithoutCancellationRequested
public void FromTask_ReturnsTokenWithoutCancellationRequested()
{
var tcs = new TaskCompletionSource();
var result = CancellationTokenHelpers.FromTask(tcs.Task);
Assert.IsTrue(result.Token.CanBeCanceled);
Assert.IsFalse(result.Token.IsCancellationRequested);
}