當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。