System.Threading.CancellationTokenSource.Cancel C# (CSharp) Méthode

Cancel() public méthode

public Cancel ( bool throwOnFirst ) : void
throwOnFirst bool
Résultat void
		public void Cancel (bool throwOnFirst)
		{
			canceled = true;
			handle.Set ();
			
			List<Exception> exceptions = null;
			if (!throwOnFirst)
				exceptions = new List<Exception> ();
			
			lock (callbacks) {
				foreach (KeyValuePair<CancellationTokenRegistration, Action> item in callbacks) {
					if (throwOnFirst) {
						item.Value ();
					} else {
						try {
							item.Value ();
						} catch (Exception e) {
							exceptions.Add (e);
						}
					}
				}
			}
			
			processed = true;
			
			if (exceptions != null && exceptions.Count > 0)
				throw new AggregateException (exceptions);
		}
		

Same methods

CancellationTokenSource::Cancel ( ) : void

Usage Example

        private static void Main(string[] args)
        {
            CancellationTokenSource cts = new CancellationTokenSource();
            Console.CancelKeyPress +=
                (sender, e) =>
                {
                    e.Cancel = true;
                    cts.Cancel();
                };

            // Since Console apps do not have a SyncronizationContext, we're leveraging the built-in support
            // in WPF to pump the messages via the Dispatcher.
            // See the following for additional details:
            //   http://blogs.msdn.com/b/pfxteam/archive/2012/01/21/10259307.aspx
            //   https://github.com/DotNetAnalyzers/StyleCopAnalyzers/pull/1362
            SynchronizationContext previousContext = SynchronizationContext.Current;
            try
            {
                var context = new DispatcherSynchronizationContext();
                SynchronizationContext.SetSynchronizationContext(context);

                DispatcherFrame dispatcherFrame = new DispatcherFrame();
                Task mainTask = MainAsync(args, cts.Token);
                mainTask.ContinueWith(task => dispatcherFrame.Continue = false);

                Dispatcher.PushFrame(dispatcherFrame);
                mainTask.GetAwaiter().GetResult();
            }
            finally
            {
                SynchronizationContext.SetSynchronizationContext(previousContext);
            }
        }
All Usage Examples Of System.Threading.CancellationTokenSource::Cancel