本文整理汇总了C#中System.Threading.CancellationTokenRegistration类的典型用法代码示例。如果您正苦于以下问题:C# CancellationTokenRegistration类的具体用法?C# CancellationTokenRegistration怎么用?C# CancellationTokenRegistration使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
CancellationTokenRegistration类属于System.Threading命名空间,在下文中一共展示了CancellationTokenRegistration类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DefaultCancellationTokenRegistration
public void DefaultCancellationTokenRegistration()
{
var registration = new CancellationTokenRegistration ();
// shouldn't throw
registration.Dispose ();
}
示例2: SaveChanges
/// <summary>
/// If AutoSaveChanges is set this method will auto commit changes
/// </summary>
/// <param name="cancellationToken"></param>
/// <returns></returns>
protected virtual Task<int> SaveChanges(CancellationToken cancellationToken)
{
var source = new TaskCompletionSource<int>();
if (AutoSaveChanges) {
var registration = new CancellationTokenRegistration();
if (cancellationToken.CanBeCanceled) {
if (cancellationToken.IsCancellationRequested) {
source.SetCanceled();
return source.Task;
}
registration = cancellationToken.Register(CancelIgnoreFailure);
}
try
{
return _uow.SaveChangesAsync(cancellationToken);
}
catch (Exception e)
{
source.SetException(e);
}
finally
{
registration.Dispose();
}
}
return source.Task;
}
示例3: TypePropertiesForCancellationTokenRegistrationAreCorrect
public void TypePropertiesForCancellationTokenRegistrationAreCorrect()
{
Assert.AreEqual(typeof(CancellationTokenRegistration).GetClassName(), "Bridge.CancellationTokenRegistration", "FullName");
object ctr = new CancellationTokenRegistration();
Assert.True(ctr is CancellationTokenRegistration, "CancellationTokenRegistration");
Assert.True(ctr is IDisposable, "IDisposable");
Assert.True(ctr is IEquatable<CancellationTokenRegistration>, "IEquatable<CancellationTokenRegistration>");
}
示例4: FromAsync
// Similar to TaskFactory.FromAsync, except it supports cancellation using ICancellableAsyncResult.
public static Task FromAsync(Func<AsyncCallback, object, ICancellableAsyncResult> beginMethod,
Action<IAsyncResult> endMethod, CancellationToken cancellationToken)
{
TaskCompletionSource<object> source = new TaskCompletionSource<object>();
CancellationTokenRegistration cancellationRegistration = new CancellationTokenRegistration();
bool cancellationRegistrationDisposed = false;
object cancellationRegistrationLock = new object();
ICancellableAsyncResult result = beginMethod.Invoke((ar) =>
{
lock (cancellationRegistrationLock)
{
cancellationRegistration.Dispose();
cancellationRegistrationDisposed = true;
}
try
{
endMethod.Invoke(ar);
source.SetResult(null);
}
catch (OperationCanceledException)
{
source.SetCanceled();
}
catch (Exception exception)
{
source.SetException(exception);
}
}, null);
lock (cancellationRegistrationLock)
{
if (!cancellationRegistrationDisposed)
{
cancellationRegistration = cancellationToken.Register(result.Cancel);
}
}
if (result.CompletedSynchronously)
{
System.Diagnostics.Debug.Assert(source.Task.IsCompleted);
}
return source.Task;
}
示例5: RSessionRequestSource
public RSessionRequestSource(bool isVisible, CancellationToken ct) {
_createRequestTcs = new TaskCompletionSourceEx<IRSessionInteraction>();
_responseTcs = new TaskCompletionSourceEx<object>();
_cancellationTokenRegistration = ct.Register(() => _createRequestTcs.TrySetCanceled(cancellationToken:ct));
IsVisible = isVisible;
}
示例6: ScheduledAsyncTask
protected ScheduledAsyncTask(AbstractScheduledEventExecutor executor, PreciseDeadline deadline,
TaskCompletionSource promise, CancellationToken cancellationToken)
: base(executor, deadline, promise)
{
_cancellationToken = cancellationToken;
_cancellationTokenRegistration = cancellationToken.Register(CancellationAction, this);
}
示例7: IrcNetworkStream
internal IrcNetworkStream(IIrcClient client)
{
_streamReader = new StreamReader(client.GetClientStream());
_streamWriter = new StreamWriter(client.GetClientStream());
_cancellationRegistration = _cancellationToken.Register(TaskRequestErrorCallback);
}
示例8: StartAsyncCore
private async Task StartAsyncCore(CancellationToken cancellationToken)
{
ListenerFactoryContext context = new ListenerFactoryContext(cancellationToken);
_listener = await _factory.CreateAsync(context);
_cancellationRegistration = _cancellationSource.Token.Register(_listener.Cancel);
await _listener.StartAsync(cancellationToken);
}
示例9: Http2ClientStream
public Http2ClientStream(int id, Priority priority, WriteQueue writeQueue, HeaderWriter headerWriter, CancellationToken cancel)
: base(id, writeQueue, headerWriter, cancel)
{
_priority = priority;
_responseTask = new TaskCompletionSource<IList<KeyValuePair<string, string>>>();
_outputStream = new OutputStream(id, _priority, writeQueue);
_cancellation = _cancel.Register(Cancel, this);
}
示例10: SchedulerOperationAwaiter
internal SchedulerOperationAwaiter(Func<Action, IDisposable> schedule, CancellationToken cancellationToken, bool postBackToOriginalContext)
{
_schedule = schedule;
_cancellationToken = cancellationToken;
_postBackToOriginalContext = postBackToOriginalContext;
_ctr = _cancellationToken.Register(Cancel);
}
示例11: ClientSelectionDialog
internal ClientSelectionDialog(MetroWindow parentWindow)
: base(parentWindow, null)
{
InitializeComponent();
this.ctlList.ItemsSource = SongInfo.LastResult;
this.m_tcs = new TaskCompletionSource<object>();
this.m_cancel = DialogSettings.CancellationToken.Register(() => this.m_tcs.TrySetResult(null));
}
示例12: VerifierDialog
internal VerifierDialog(MainWindow parentWindow)
: base(parentWindow, null)
{
InitializeComponent();
this.m_mainwindow = parentWindow;
this.m_tcs = new TaskCompletionSource<object>();
this.m_cancel = DialogSettings.CancellationToken.Register(() => this.m_tcs.TrySetResult(null));
}
示例13: RunAsync
public Task RunAsync(IEnumerable<ITest> selectedTests, IProgress<double> progress, TextWriter output, CancellationToken cancellationToken)
{
this.progress = progress;
this.output = output;
progressPerTest = 1.0 / GetExpectedNumberOfTestResults(selectedTests);
tcs = new TaskCompletionSource<object>();
Start(selectedTests);
cancellationTokenRegistration = cancellationToken.Register(Cancel, true);
return tcs.Task;
}
示例14: ADSelectionDialog
internal ADSelectionDialog(MetroWindow parentWindow)
: base(parentWindow, null)
{
InitializeComponent();
this.ctlList.ItemsSource = IParseRule.RulesPlayer;
this.m_tcs = new TaskCompletionSource<object>();
this.m_cancel = DialogSettings.CancellationToken.Register(() => this.m_tcs.TrySetResult(null));
}
示例15: StopSignal
public StopSignal()
{
_stopRequested = new TaskCompletionSource<bool>();
_stopToken = new CancellationTokenSource();
_registration = _stopToken.Token.Register(() =>
{
_stopRequested.TrySetResult(true);
_registration.Dispose();
});
}