本文整理汇总了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;
}
示例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;
}
示例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();
}
}
}
示例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;
}
示例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);
}
};
}
示例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;
}
示例7: TrySetCanceled_CancelsTask
public void TrySetCanceled_CancelsTask()
{
var tcs = new TaskCompletionSource();
tcs.TrySetCanceled();
Assert.IsTrue(tcs.Task.IsCanceled);
}