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 itsRunWorkerCompleted
andDoWork
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
- WaitForm1.cs (VB: WaitForm1.vb)
- Form1.cs (VB: Form1.vb)
- ILocked.cs (VB: ILocked.vb)
- Locker.cs (VB: Locker.vb)
Documentation
Does this example address your development requirements/objectives?
(you will be redirected to DevExpress.com to submit your response)
Example Code
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;
}
}
}
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);
}
}
}
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; }
}
}
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;
}
}
}