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


C# TaskCompletionSource.TrySetCanceled方法代码示例

本文整理汇总了C#中Nito.AsyncEx.TaskCompletionSource.TrySetCanceled方法的典型用法代码示例。如果您正苦于以下问题:C# TaskCompletionSource.TrySetCanceled方法的具体用法?C# TaskCompletionSource.TrySetCanceled怎么用?C# TaskCompletionSource.TrySetCanceled使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Nito.AsyncEx.TaskCompletionSource的用法示例。


在下文中一共展示了TaskCompletionSource.TrySetCanceled方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

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

示例2: AsTask

 /// <summary>
 /// Returns a <see cref="Task"/> that is canceled when this <see cref="CancellationToken"/> is canceled. This method will leak resources if the cancellation token is long-lived; use <see cref="ToCancellationTokenTaskSource"/> for a similar approach with proper resource management.
 /// </summary>
 /// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor.</param>
 /// <returns>A <see cref="Task"/> that is canceled when this <see cref="CancellationToken"/> is canceled.</returns>
 public static Task AsTask(this CancellationToken cancellationToken)
 {
     if (!cancellationToken.CanBeCanceled)
         return TaskConstants.Never;
     if (cancellationToken.IsCancellationRequested)
         return TaskConstants.Canceled;
     var tcs = new TaskCompletionSource();
     cancellationToken.Register(() => tcs.TrySetCanceled(), useSynchronizationContext: false);
     return tcs.Task;
 }
开发者ID:Lakerfield,项目名称:AsyncEx,代码行数:15,代码来源:CancellationTokenExtensions.cs

示例3: ConfigureTopologyAsync

        public static async Task ConfigureTopologyAsync(this Link @this,
            Func<ILinkTopologyConfig, Task> configure, CancellationToken cancellationToken)
        {            
            var completion = new TaskCompletionSource();
            if (cancellationToken.IsCancellationRequested)
            {
                completion.TrySetCanceled();
                await completion.Task;
                return;                
            }

            using (@this.CreateTopologyConfigurator(configure, () =>
            {
                completion.TrySetResultWithBackgroundContinuations();
                return Task.FromResult((object) null);
            }, ex =>
            {
                completion.TrySetExceptionWithBackgroundContinuations(ex);
                return Task.FromResult((object) null);
            }))
            {
                IDisposable registration = null;
                try
                {
                    registration = cancellationToken.Register(() =>
                    {
                        completion.TrySetCanceledWithBackgroundContinuations();                    
                    });
                }
                catch (ObjectDisposedException)
                {
                    // Cancellation source already disposed                  
                }

                if (cancellationToken.IsCancellationRequested)
                {
                    completion.TrySetCanceledWithBackgroundContinuations();
                }

                try
                {
                    await completion.Task
                        .ConfigureAwait(false);
                }
                finally
                {
                    registration?.Dispose();
                }
            }
        }
开发者ID:rabbit-link,项目名称:rabbit-link,代码行数:50,代码来源:LinkExtensions.cs

示例4: CancellationTokenTaskSource

 /// <summary>
 /// Creates a task for the specified cancellation token, registering with the token if necessary.
 /// </summary>
 /// <param name="cancellationToken">The cancellation token to observe.</param>
 public CancellationTokenTaskSource(CancellationToken cancellationToken)
 {
     if (!cancellationToken.CanBeCanceled)
     {
         Task = TaskConstants.Never;
         return;
     }
     if (cancellationToken.IsCancellationRequested)
     {
         Task = TaskConstants.Canceled;
         return;
     }
     var tcs = new TaskCompletionSource();
     _registration = cancellationToken.Register(() => tcs.TrySetCanceled(), useSynchronizationContext: false);
     Task = tcs.Task;
 }
开发者ID:Lakerfield,项目名称:AsyncEx,代码行数:20,代码来源:CancellationTokenExtensions.cs

示例5: Callback

 private static AsyncCallback Callback(Action<IAsyncResult> endMethod, TaskCompletionSource<object> tcs)
 {
     return asyncResult =>
     {
         try
         {
             endMethod(asyncResult);
             tcs.TrySetResult(null);
         }
         catch (OperationCanceledException)
         {
             tcs.TrySetCanceled();
         }
         catch (Exception ex)
         {
             tcs.TrySetException(ex);
         }
     };
 }
开发者ID:Lakerfield,项目名称:AsyncEx,代码行数:19,代码来源:AsyncFactory.cs

示例6: DumpWebPageAsync

        private static Task<string> DumpWebPageAsync(WebClient client, Uri uri)
        {
            //si utilizza TaskCompletionSource che gestisce un task figlio
            var tcs = new TaskCompletionSource<string>();

            DownloadStringCompletedEventHandler handler = null;

            handler = (sender, args) =>
            {
                client.DownloadStringCompleted -= handler;

                if (args.Cancelled)
                    tcs.TrySetCanceled();
                else if (args.Error != null)
                    tcs.TrySetException(args.Error);
                else
                    tcs.TrySetResult(args.Result);
            };

            client.DownloadStringCompleted += handler;
            client.DownloadStringAsync(uri);

            return tcs.Task;
        }
开发者ID:andrekiba,项目名称:KLabAsyncAwait,代码行数:24,代码来源:APM_EAP.cs

示例7: TrySetCanceled_CancelsTask

 public void TrySetCanceled_CancelsTask()
 {
     var tcs = new TaskCompletionSource();
     tcs.TrySetCanceled();
     Assert.IsTrue(tcs.Task.IsCanceled);
 }
开发者ID:Lakerfield,项目名称:AsyncEx,代码行数:6,代码来源:TaskCompletionSourceUnitTests.cs


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