当前位置: 首页>>代码示例>>C#>>正文


C# AsyncEx.TaskCompletionSource类代码示例

本文整理汇总了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;
            });
        }
开发者ID:Lakerfield,项目名称:AsyncEx,代码行数:33,代码来源:AsyncLockUnitTests.cs

示例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;
            });
        }
开发者ID:Lakerfield,项目名称:AsyncEx,代码行数:29,代码来源:AsyncLockUnitTests.cs

示例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);
            });
        }
开发者ID:Lakerfield,项目名称:AsyncEx,代码行数:26,代码来源:PropertyProgressUnitTests.cs

示例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;
        }
开发者ID:pglazkov,项目名称:Linqua,代码行数:28,代码来源:DispatcherServiceExtensions.cs

示例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);
 }
开发者ID:Lakerfield,项目名称:AsyncEx,代码行数:11,代码来源:AsyncBarrier.cs

示例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;
            });
        }
开发者ID:Lakerfield,项目名称:AsyncEx,代码行数:26,代码来源:AsyncMonitorUnitTests.cs

示例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);
 }
开发者ID:Lakerfield,项目名称:AsyncEx,代码行数:8,代码来源:TaskCompletionSourceUnitTests.cs

示例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);
 }
开发者ID:Lakerfield,项目名称:AsyncEx,代码行数:8,代码来源:TaskCompletionSourceUnitTests.cs

示例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));
 }
开发者ID:Lakerfield,项目名称:AsyncEx,代码行数:8,代码来源:TaskCompletionSourceUnitTests.cs

示例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;
		}
开发者ID:dance2die,项目名称:Demo.AsyncTest,代码行数:8,代码来源:Program.cs

示例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();
     }
 }
开发者ID:Nucs,项目名称:nlib,代码行数:12,代码来源:AsyncManualResetEvent.cs

示例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();
 }
开发者ID:Lakerfield,项目名称:AsyncEx,代码行数:8,代码来源:AsyncBarrierUnitTests.cs

示例13: 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

示例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);
     });
 }
开发者ID:Lakerfield,项目名称:AsyncEx,代码行数:9,代码来源:TaskCompletionSourceExtensionsUnitTests.cs

示例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);
        }
开发者ID:Lakerfield,项目名称:AsyncEx,代码行数:9,代码来源:CancellationTokenHelpersUnitTests.cs


注:本文中的Nito.AsyncEx.TaskCompletionSource类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。