The .Net allows you to develop and use in your applications new user-defined controls, which can considerably simplify our coding. First of all we should define the kind of the control we want to discuss.
In Microsoft documentation it is specified that Windows Forms support three kinds of user-defined controls:
- Composite
- Extended
- Custom
A composite controls combine more than one Windows Forms control encapsulated in one unit (a common container). They inherit from the System.Windows.Forms.UserControl class and often are called user controls.
An extended control often are called derived or inherited control. We use this control when most of the functionality we need is already identical to an existing Windows Forms control. We just create a class that inherits from the class (Windows Forms control) we have chosen, and overrides or adds properties and methods.
A custom control should be created from the beginning by inheriting from the Control class. Here we are going to discuss composite user control (or in short: user control).
Using the BackgroundWorker Component allows to run an operation asynchronously. In this article I will show how you can use the BackgroundWorker Component with user controls. The examples are written using C#.
Let's create a small project, named "BGW_UC" (that stands for "BackgroundWorker Component and User Controls"). The project includes one windows form, named "Form1", and one folder, named "UC" (that stands for "User Controls"). First of all we add to the UC folder some base control, named "UC_0". Then we add two user controls ("UC_1" and "UC_2"), which inherit from the base control UC_0. You can make it by two ways: or by means of the inheritance picker (fig. 1) or simply to change a line of your code (fig. 2):
Figure 1.
Figure 2.
Now our project looks like this (fig.3):
Figure 3.
We add the BackgroundWorker Component (named "bgw") only to our base control. The UC_1 and UC_2 controls (the derived user controls) gain all the non-private data and behavior (including, of course, the bgw) of the base control (UC_0) in addition to any other data or behaviors they define for themselves.
As we have known, the BackgroundWorker Component has three events: DoWork, ProgressChanged and RunWorkerCompleted. We have to use these events in our controls. In addition to it, on some form, where we use our controls, we wish to catch the moment, when these events occur. With this purpose we, first of all, add to the UC_0 the events (for simplicity we add only two events):
- public event EventHandler BGWDoWork;
- public event EventHandler BGWCompleted;
Then we change default methods
- private void bgw_DoWork(object sender, DoWorkEventArgs e)
to
- protected virtual void bgw_DoWork(object sender, DoWorkEventArgs e)
and so on.
The protected keyword allows to access method within its class and by derived classes, and the virtual keyword allows the methods to be overridden in derived classes. In order to run the RunWorkerAsync method of the BackgroundWorker Component we add the method:
- protected void RunWoker()
- {
- bgw.RunWorkerAsync();
- }
The full text (code) of our base control
UC_0 will be the following:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Drawing;
- using System.Data;
- using System.Text;
- using System.Windows.Forms;
-
- namespace BGW_UC.UC
- {
- public partial class UC_0 : UserControl
- {
- public UC_0()
- {
- InitializeComponent();
- }
- #region ForClass
- public event EventHandler BGWDoWork;
- public event EventHandler BGWCompleted;
- #endregion
- protected virtual void bgw_DoWork(object sender, DoWorkEventArgs e)
- {
- if (BGWDoWork != null)
- {
- BGWDoWork(this, e);
- }
- }
- protected virtual void bgw_ProgressChanged(object sender, ProgressChangedEventArgs e)
- {
- }
- protected virtual void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
- {
- if (BGWCompleted != null)
- {
- BGWCompleted(this, e);
- }
- }
- protected void RunWoker()
- {
- bgw.RunWorkerAsync();
- }
- }
- }
OK! Everything is ready to add some functionality to the UC_1 and UC_2.
First of all we add the override methods bgw_DoWork, bgw_ProgressChanged and bgw_RunWorkerCompleted (fig. 4).
Figure 4.
To start our asynchronous operation we use for the UC_1 the method
- public void LoadData()
- {
- base.RunWoker();
- }
And for the UC_2 the click_button event (preliminary having added the button1 to the UC_2):
- private void button1_Click(object sender, EventArgs e)
- {
- base.RunWoker();
- }
In order to simulate database transactions (of course, you can connect to real database; this simulation is only for our test purpose) we use GetData.dll. With this purpose we add reference to GetData.dll and add two lines of code to the bgw_DoWork method:
- GetData.GetDataHelp getData = new GetData.GetDataHelp();
- DataTable dt = getData.getDataSetCities(1000000).Tables[0];
The full text (code) of the
UC_1 control will be the following:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
-
- namespace BGW_UC.UC
- {
- public partial class UC_1 : BGW_UC.UC.UC_0
- {
- public UC_1()
- {
- InitializeComponent();
- }
- protected override void bgw_DoWork(object sender, DoWorkEventArgs e)
- {
- base.bgw_DoWork(sender, e);
- GetData.GetDataHelp getData = new GetData.GetDataHelp();
- DataTable dt = getData.getDataSetCities(1000000).Tables[0];
- }
- protected override void bgw_ProgressChanged(object sender, ProgressChangedEventArgs e)
- {
- base.bgw_ProgressChanged(sender, e);
- }
- protected override void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
- {
- base.bgw_RunWorkerCompleted(sender, e);
- }
- public void LoadData()
- {
- base.RunWoker();
- }
- }
- }
The full text (code) of the
UC_2 control will be the following:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Drawing;
- using System.Data;
- using System.Text;
- using System.Windows.Forms;
-
- namespace BGW_UC.UC
- {
- public partial class UC_2 : BGW_UC.UC.UC_0
- {
- public UC_2()
- {
- InitializeComponent();
- }
- protected override void bgw_DoWork(object sender, DoWorkEventArgs e)
- {
- base.bgw_DoWork(sender, e);
- GetData.GetDataHelp getData = new GetData.GetDataHelp();
- DataTable dt = getData.getDataSetCities(1000000).Tables[0];
- }
- protected override void bgw_ProgressChanged(object sender, ProgressChangedEventArgs e)
- {
- base.bgw_ProgressChanged(sender, e);
- }
- protected override void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
- {
- base.bgw_RunWorkerCompleted(sender, e);
- }
- private void button1_Click(object sender, EventArgs e)
- {
- base.RunWoker();
- }
- }
- }
OK! Now we add to the Form1 the follow control: UC_1 (named "UC_11"), UC_2 (UC_21), button (button1), statusStrips (statusStrip1 and statusStrip2); to the statusStrip1 we add toolStripStatusLabel1 and to the statusStrip2-toolStripStatusLabel2.
To catch the beginning and finish of the data loading we use the BGWDoWork and BGMCompleted events (fig. 5):
Figure 5.
The full text (code) of the Form1 will be the following:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
-
- namespace BGW_UC
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- uC_11.LoadData();
- }
- private void uC_11_BGWCompleted(object sender, EventArgs e)
- {
- toolStripStatusLabel1.Text = "End...UC_1";
- }
- private void uC_11_BGWDoWork(object sender, EventArgs e)
- {
- toolStripStatusLabel1.Text = "Load...UC_1";
- }
- private void uC_21_BGWCompleted(object sender, EventArgs e)
- {
- toolStripStatusLabel2.Text = "End...UC_2";
- }
- private void uC_21_BGWDoWork(object sender, EventArgs e)
- {
- toolStripStatusLabel2.Text = "Load...UC_2";
- }
- }
- }
As you can see, now we have the possibility to test two independent time-consuming processes: the first one we start with the help of the button of the Form1 (for the UC_1), the second process we start with the help of the button of the control itself (for the UC_2). Run the project, click on the "Load UC_1" button. You can see that the first loading starts. But our user interface does not hang. Click on the "Load UC_2" button. Now two processes are running (fig. 6).
Figure 6.
In some seconds the first process comes to an end (fig. 7) and then the second (fig. 8).
Figure 7.
![img8.gif]()
Figure 8.