/// <summary> /// <paramref name="source"/>를 취소하고, <see cref="OperationCanceledException"/>을 발생시키도록 합니다. /// </summary> /// <param name="source">The source to be canceled.</param> public static void CancelAndThrow(this CancellationTokenSource source) { if(IsDebugEnabled) log.Debug("취소 작업을 수행하고, 예외를 발생시킵니다."); source.Cancel(); source.Token.ThrowIfCancellationRequested(); }
public static void TryCancel(this CancellationTokenSource cancellationTokenSource) { try { cancellationTokenSource.Cancel(); } catch(ObjectDisposedException) { } }
/// <summary> /// An exception free <see cref="DbCommand.Cancel()"/> to workaround issues with drivers, notably MySQL. /// </summary> /// <param name="command">The command.</param> public static void SafeCancel(this DbCommand command) { try { command.Cancel(); } catch (DbException e) { _log.Warn("Error while trying to cancel the command. In some cases, e.g. MySQL, it is safe to ignore the error.", e); } }
public static void CancelAfter(this CancellationTokenSource source, int dueTime) { var timer = new Timer(delegate(object self) { ((IDisposable)self).Dispose(); try { source.Cancel(); } catch (ObjectDisposedException) { } }); timer.Change(dueTime, -1); }
/// <summary> /// Привязать отмену к времени жизни страницы. /// </summary> /// <param name="viewModel">Модель представления.</param> public static void BindCancelToPageLifeTime(this ICancellableViewModel viewModel) { if (viewModel == null) { return; } var page = Shell.HamburgerMenu?.NavigationService?.FrameFacade?.Content as IPageLifetimeCallback; if (page == null) { return; } page.NavigatedFrom += CreateCallback(new WeakReference<ICancellableViewModel>(viewModel), new WeakReference<IPageLifetimeCallback>(page)); EventHandler<NavigationEventArgs> callback = null; callback = (sender, e) => { viewModel.Cancel(); if (callback != null) page.NavigatedFrom -= callback; }; page.NavigatedFrom += callback; }
public static void CancelAfter(this CancellationTokenSource cancellationTokenSource, TimeSpan delay) { Timer timer = null; object disposeLock = new object(); TimerCallback callback = state => { try { cancellationTokenSource.Cancel(); } catch(Exception e) { Trace.TraceWarning(string.Format("[{0}] Failed to cancel cancelation token source. {1}", typeof(CancellationTokenSourceExtensionMethods), e)); } finally { lock (disposeLock) { DisposeHelper.SilentDispose(timer); } } }; cancellationTokenSource.Token.Register(() => { lock (disposeLock) { DisposeHelper.DisposeIfNotNull(timer); } }); timer = new Timer(callback, null, Timeout.Infinite, Timeout.Infinite); timer.Change(delay.ToMillisecondsTimeout(), Timeout.Infinite); if(cancellationTokenSource.Token.IsCancellationRequested) { lock (disposeLock) { timer.Dispose(); } } }
/// <summary> /// Performs the given action as a transaction with the given name</summary> /// <param name="context">Transaction context or null</param> /// <param name="transaction">Transaction action</param> /// <param name="transactionName">Transaction name</param> /// <returns>True if the transaction succeeded and false if it was cancelled (i.e., /// InvalidTransactionException was thrown)</returns> /// <remarks>In the implementation of 'transaction', throw InvalidTransactionException /// to cancel the transaction and log a warning message to the user (unless the /// InvalidTransactionException's ReportError is false).</remarks> public static bool DoTransaction(this ITransactionContext context, Action transaction, string transactionName) { // If we are already in a transaction just perform the action // Let all exceptions "bubble up" and be handled by the outer transaction if (context != null && context.InTransaction) { transaction(); return true; } try { if (context != null) context.Begin(transactionName); //Transactions can be canceled in the call to Begin. When this occurs, //we want to skip doing the transaction and the end calls. if (context != null && !context.InTransaction) return false; transaction(); if (context != null) context.End(); } catch (InvalidTransactionException ex) { if (context != null && context.InTransaction) context.Cancel(); if (ex.ReportError) Outputs.WriteLine(OutputMessageType.Error, ex.Message); return false; } return true; }
/// <summary> /// If <paramref name="cancelable"/> is not <c>null</c> it's canceled. /// </summary> /// <param name="cancelable">The cancelable. Will be canceled if it's not <c>null</c></param> public static void CancelIfNotNull(this ICancelable cancelable) { if(cancelable != null) cancelable.Cancel(); }
/// <summary>Cancels a CancellationTokenSource and throws a corresponding OperationCanceledException.</summary> /// <param name="source">The source to be canceled.</param> public static void CancelAndThrow(this CancellationTokenSource source) { source.Cancel(); source.Token.ThrowIfCancellationRequested(); }
public static void ExecuteAndCancel(this ITask task, params Action[] actions) { if (task.Execute(actions)) task.Cancel(); }
/// <summary> /// Cancel without throwing any exceptions. /// </summary> public static void CancelSafe(this CancellationTokenSource cancellationTokenSource) { if (null == cancellationTokenSource) return; try { if (!cancellationTokenSource.IsCancellationRequested) cancellationTokenSource.Cancel(); } catch (Exception ex) { Debug.WriteLine("CancellationTokenExtensions.CancelSafe() failed: " + ex.Message); } }
/// <summary> /// Cancel without throwing any exceptions on the default task scheduler. /// </summary> public static void BackgroundCancelSafe(this CancellationTokenSource cancellationTokenSource) { if (null == cancellationTokenSource) return; try { if (!cancellationTokenSource.IsCancellationRequested) { var t = Task.Run(() => { try { cancellationTokenSource.Cancel(); } catch (Exception ex) { Debug.WriteLine("CancellationTokenExtensions.BackgroundCancelSafe() cancel failed: " + ex.Message); } }); TaskCollector.Default.Add(t, "CancellationTokenExtensions BackgroundCancelSafe"); } } catch (Exception ex) { Debug.WriteLine("CancellationTokenExtensions.BackgroundCancelSafe() failed: " + ex.Message); } }
private static void CancelIgnoreFailure(this OracleCommand command) { try { command.Cancel(); } catch (Exception e) { Trace.WriteLine($"Command cancellation failed: {e}"); } }