System.ComponentModel.BackgroundWorker.CancelAsync C# (CSharp) Method

CancelAsync() public method

public CancelAsync ( ) : void
return void
        public void CancelAsync()
        {
            if (!WorkerSupportsCancellation)
            {
                throw new InvalidOperationException(SR.GetString(SR.BackgroundWorker_WorkerDoesntSupportCancellation));
            }

            cancellationPending = true;
        }

Usage Example

Beispiel #1
1
        public Window4()
        {
            InitializeComponent();
            this.PlotModel = new PlotModel();
            this.PlotModel.Series.Add(new FunctionSeries());
            DataContext = this;
            var worker = new BackgroundWorker { WorkerSupportsCancellation = true };
            double x = 0;
            worker.DoWork += (s, e) =>
            {
                while (!worker.CancellationPending)
                {
                    lock (this.PlotModel.SyncRoot)
                    {
                        this.PlotModel.Title = "Plot updated: " + DateTime.Now;
                        this.PlotModel.Series[0] = new FunctionSeries(Math.Sin, x, x + 4, 0.01);
                    }
                    x += 0.1;
                    PlotModel.InvalidatePlot(true);
                    Thread.Sleep(100);
                }
            };

            worker.RunWorkerAsync();
            this.Closed += (s, e) => worker.CancelAsync();
        }
All Usage Examples Of System.ComponentModel.BackgroundWorker::CancelAsync