Example E4524
Visible to All Users

WinForms Wait Form - How to cancel a time-consuming operation

This example shows how to close the Wait Form to cancel a time-consuming operation:

  • Add a GridControl onto the Form.
  • Place a BackgroundWorker component onto the Form and handle its RunWorkerCompleted and DoWork events.
  • Place a SplashScreenManager component onto the Form.
  • Invoke the SplashScreen Manager's smart tag menu and add a Wait Form.
  • Add a Load Data button onto the Form and handle its Click event to load data and display a wait form.
    C#
    private void simpleButton1_Click(object sender, EventArgs e) { Person.counter = 0; backgroundWorker1.RunWorkerAsync(locker1); splashScreenManager1.ShowWaitForm(); splashScreenManager1.SendCommand(WaitForm1.WaitFormCommand.SendObject, locker1); }
  • Open the WaitForm designer and add a Cancel button.
  • Handle the Cancel button's Click event to cancel the operation:
    C#
    private void simpleButton1_Click(object sender, EventArgs e) { if (locker != null) ((ILocked)locker).IsCanceled = true; }

Files to Review

Documentation

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

WaitFormCanceling/WaitForm1.cs(vb)
C#
// Developer Express Code Central Example: // How to cancel time-consuming operation from the WaitForm // // This example illustrates how to cancel a time-consuming operation on a WaitForm. // To accomplish this task, place a BackgroundWorker and SplashScreenManager onto a // Form, and add the Cancel button in the WaitForm's designer. The main idea is to // perform data loading in a background thread, pass the ILocked object both to the // WaitForm and to this thread, and check its condition while data is loading. // // You can find sample updates and versions for different programming languages here: // http://www.devexpress.com/example=E4524 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using DevExpress.XtraWaitForm; using System.Threading; namespace WaitFormCanceling { public partial class WaitForm1 : WaitForm { public WaitForm1() { InitializeComponent(); this.progressPanel1.AutoHeight = true; } object locker; #region Overrides public override void SetCaption(string caption) { base.SetCaption(caption); this.progressPanel1.Caption = caption; } public override void SetDescription(string description) { base.SetDescription(description); this.progressPanel1.Description = description; } public override void ProcessCommand(Enum cmd, object arg) { base.ProcessCommand(cmd, arg); WaitFormCommand command = (WaitFormCommand)cmd; if (command == WaitFormCommand.SendObject) { locker = arg; } } #endregion public enum WaitFormCommand { SendObject } private void simpleButton1_Click(object sender, EventArgs e) { if(locker!=null) ((ILocked)locker).IsCanceled = true; } } }
WaitFormCanceling/Form1.cs(vb)
C#
// Developer Express Code Central Example: // How to cancel time-consuming operation from the WaitForm // // This example illustrates how to cancel a time-consuming operation on a WaitForm. // To accomplish this task, place a BackgroundWorker and SplashScreenManager onto a // Form, and add the Cancel button in the WaitForm's designer. The main idea is to // perform data loading in a background thread, pass the ILocked object both to the // WaitForm and to this thread, and check its condition while data is loading. // // You can find sample updates and versions for different programming languages here: // http://www.devexpress.com/example=E4524 using DevExpress.XtraEditors; using System; using System.ComponentModel; using System.Windows.Forms; namespace WaitFormCanceling { public partial class Form1 : XtraForm { public Form1() { InitializeComponent(); backgroundWorker1.WorkerSupportsCancellation = true; backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork); backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted); } Locker locker1 = new Locker(); void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { splashScreenManager1.CloseWaitForm(); locker1.IsCanceled = false; if (e.Error!=null) { MessageBox.Show(e.Error.Message); return; } else { gridControl.DataSource = (BindingList<Person>)e.Result; label1.Text = ((BindingList<Person>)e.Result).Count.ToString() + " records from 3 000 005 are loaded"; } } void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker worker = sender as BackgroundWorker; if (worker != null) { BindingList<Person> gridDataList = new BindingList<Person>(); gridDataList.Add(new Person("John", "Smith")); gridDataList.Add(new Person("Gabriel", "Smith")); gridDataList.Add(new Person("Ashley", "Smith", "some comment")); gridDataList.Add(new Person("Adrian", "Smith", "some comment")); gridDataList.Add(new Person("Gabriella", "Smith", "some comment")); for (int i = 0; i < 3000000; i++) { gridDataList.Add(new Person()); if (((ILocked)e.Argument).IsCanceled) { break; } } e.Result = gridDataList; } } private void simpleButton1_Click(object sender, EventArgs e) { Person.counter = 0; backgroundWorker1.RunWorkerAsync(locker1); splashScreenManager1.ShowWaitForm(); splashScreenManager1.SendCommand(WaitForm1.WaitFormCommand.SendObject, locker1); } } }
WaitFormCanceling/ILocked.cs(vb)
C#
// Developer Express Code Central Example: // How to cancel time-consuming operation from the WaitForm // // This example illustrates how to cancel a time-consuming operation on a WaitForm. // To accomplish this task, place a BackgroundWorker and SplashScreenManager onto a // Form, and add the Cancel button in the WaitForm's designer. The main idea is to // perform data loading in a background thread, pass the ILocked object both to the // WaitForm and to this thread, and check its condition while data is loading. // // You can find sample updates and versions for different programming languages here: // http://www.devexpress.com/example=E4524 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using DevExpress.Skins; using DevExpress.LookAndFeel; using DevExpress.UserSkins; using DevExpress.XtraEditors; using DevExpress.XtraSplashScreen; namespace WaitFormCanceling { public interface ILocked { bool IsCanceled { get; set; } } }
WaitFormCanceling/Locker.cs(vb)
C#
// Developer Express Code Central Example: // How to cancel time-consuming operation from the WaitForm // // This example illustrates how to cancel a time-consuming operation on a WaitForm. // To accomplish this task, place a BackgroundWorker and SplashScreenManager onto a // Form, and add the Cancel button in the WaitForm's designer. The main idea is to // perform data loading in a background thread, pass the ILocked object both to the // WaitForm and to this thread, and check its condition while data is loading. // // You can find sample updates and versions for different programming languages here: // http://www.devexpress.com/example=E4524 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using DevExpress.Skins; using DevExpress.LookAndFeel; using DevExpress.UserSkins; using DevExpress.XtraEditors; using DevExpress.XtraSplashScreen; namespace WaitFormCanceling { public class Locker : ILocked { //// Fields... private bool _IsCanceled; public bool IsCanceled { get { return _IsCanceled; } set { _IsCanceled = value; } } public Locker() { _IsCanceled = false; } } }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.