本文整理汇总了C#中Nito.AsyncEx.TaskCompletionSource.SetResult方法的典型用法代码示例。如果您正苦于以下问题:C# TaskCompletionSource.SetResult方法的具体用法?C# TaskCompletionSource.SetResult怎么用?C# TaskCompletionSource.SetResult使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Nito.AsyncEx.TaskCompletionSource
的用法示例。
在下文中一共展示了TaskCompletionSource.SetResult方法的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: 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;
});
}
示例4: 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);
});
}
示例5: 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();
}
}
示例6: 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;
}
示例7: 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();
}
示例8: SynchronouslyEndingOperation
private Task SynchronouslyEndingOperation()
{
Synchronous.Log("Entered synchronously ending operation");
Thread.Sleep(100);
var tcs = new TaskCompletionSource();
tcs.SetResult();
Synchronous.Log("Leaving synchronously ending operation");
return tcs.Task;
}
示例9: FromTaskSynchronously_TaskCompletes_TokenGetsCancellationRequested
public void FromTaskSynchronously_TaskCompletes_TokenGetsCancellationRequested()
{
var tcs = new TaskCompletionSource();
var result = CancellationTokenHelpers.FromTask(tcs.Task, TaskContinuationOptions.ExecuteSynchronously);
tcs.SetResult();
Assert.IsTrue(result.Token.CanBeCanceled);
Assert.IsTrue(result.Token.IsCancellationRequested);
}
示例10: Pulse_ReleasesOneWaiter
public void Pulse_ReleasesOneWaiter()
{
AsyncContext.Run(async () =>
{
var monitor = new AsyncMonitor();
int completed = 0;
var task1Ready = new TaskCompletionSource();
var task2Ready = new TaskCompletionSource();
Task<IDisposable> lockTask1 = null;
Task waitTask1 = null;
var task1 = Task.Run(async () =>
{
lockTask1 = monitor.EnterAsync();
using (await lockTask1)
{
waitTask1 = monitor.WaitAsync();
task1Ready.SetResult();
await waitTask1;
Interlocked.Increment(ref completed);
}
});
await task1Ready.Task;
Task<IDisposable> lockTask2 = null;
Task waitTask2 = null;
var task2 = Task.Run(async () =>
{
lockTask2 = monitor.EnterAsync();
using (await lockTask2)
{
waitTask2 = monitor.WaitAsync();
task2Ready.SetResult();
await waitTask2;
Interlocked.Increment(ref completed);
}
});
await task2Ready.Task;
Task<IDisposable> lockTask3 = monitor.EnterAsync();
using (await lockTask3)
{
monitor.Pulse();
}
await Task.WhenAny(task1, task2);
var result = Interlocked.CompareExchange(ref completed, 0, 0);
Assert.AreEqual(1, result);
});
}
示例11: AsyncLazy_Start_CallsFunc
public void AsyncLazy_Start_CallsFunc()
{
AsyncContext.Run(async () =>
{
var tcs = new TaskCompletionSource();
Func<int> func = () =>
{
tcs.SetResult();
return 13;
};
var lazy = new AsyncLazy<int>(func);
lazy.Start();
await tcs.Task;
});
}
示例12: AsyncLock_DoubleDispose_OnlyPermitsOneTask
public void AsyncLock_DoubleDispose_OnlyPermitsOneTask()
{
AsyncContext.Run(async () =>
{
var mutex = new AsyncLock();
var task1HasLock = new TaskCompletionSource();
var task1Continue = new TaskCompletionSource();
Task<IDisposable> lockTask = null;
await Task.Run(async () =>
{
lockTask = mutex.LockAsync();
var key = await lockTask;
key.Dispose();
key.Dispose();
});
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;
});
}
示例13: Pulse_ReleasesOneWaiter
public void Pulse_ReleasesOneWaiter()
{
Test.Async(async () =>
{
var monitor = new AsyncMonitor();
int completed = 0;
var task1Ready = new TaskCompletionSource();
var task2Ready = new TaskCompletionSource();
var task1 = TaskShim.Run(async () =>
{
using (await monitor.EnterAsync())
{
var waitTask1 = monitor.WaitAsync();
task1Ready.SetResult();
await waitTask1;
Interlocked.Increment(ref completed);
}
});
await task1Ready.Task;
var task2 = TaskShim.Run(async () =>
{
using (await monitor.EnterAsync())
{
var waitTask2 = monitor.WaitAsync();
task2Ready.SetResult();
await waitTask2;
Interlocked.Increment(ref completed);
}
});
await task2Ready.Task;
using (await monitor.EnterAsync())
{
monitor.Pulse();
}
await TaskShim.WhenAny(task1, task2);
var result = Interlocked.CompareExchange(ref completed, 0, 0);
Assert.AreEqual(1, result);
});
}
示例14: WriteLocked_Unlocked_PermitsAnotherWriterLock
public void WriteLocked_Unlocked_PermitsAnotherWriterLock()
{
AsyncContext.Run(async () =>
{
var rwl = new AsyncReaderWriterLock();
var firstWriteLockTaken = new TaskCompletionSource();
var releaseFirstWriteLock = new TaskCompletionSource();
var task = Task.Run(async () =>
{
using (await rwl.WriterLockAsync())
{
firstWriteLockTaken.SetResult();
await releaseFirstWriteLock.Task;
}
});
await firstWriteLockTaken.Task;
var lockTask = rwl.WriterLockAsync();
Assert.IsFalse(lockTask.IsCompleted);
releaseFirstWriteLock.SetResult();
await lockTask;
});
}
示例15: Locked_PreventsLockUntilUnlocked
public void Locked_PreventsLockUntilUnlocked()
{
Test.Async(async () =>
{
var monitor = new AsyncMonitor();
var task1HasLock = new TaskCompletionSource();
var task1Continue = new TaskCompletionSource();
var task1 = TaskShim.Run(async () =>
{
using (await monitor.EnterAsync())
{
task1HasLock.SetResult();
await task1Continue.Task;
}
});
await task1HasLock.Task;
var lockTask = monitor.EnterAsync().AsTask();
Assert.IsFalse(lockTask.IsCompleted);
task1Continue.SetResult();
await lockTask;
});
}