1. Working steps of BackGroundWorker
1. Drag a BackGroundWorker control into the form.
2. Call the RunWorkerAsync () method of BackGroundWorker in a method or event.
3. This method is asynchronous and will automatically trigger the DoWork event of the BackGroundWorker.
4. Calling the ReportProgress method will cause the ProgressChanged event.
2. Example of using BackGroundWorker
Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Text;
Using System. Windows. Forms;
Using System. Threading;
Using System. Data. SqlClient;
// This case requires an SQL Server database named bgwTestDB
// The database should contain the tbBgwTest table.
// The table contains two columns: data1 and data2.
// A stored procedure is also required in the database. The SQL statement is as follows:
/***************
Create procedure insertOneData
@ Data1 nchar (10 ),
@ Data2 int
As
Insert into tbBgwTest (data1, data2) values (@ data1, @ data2)
********************/
Namespace winBackgroundWorkerTest
{
Public partial class backgroundWorkerTest: Form
{
Int count = 30;
Public backgroundWorkerTest ()
{
InitializeComponent ();
}
Private void btnAdd_Click (object sender, EventArgs e)
{
// 1. Call the RunWorkerAsync method of bgwInsertData to trigger a DoWork event
BgwInsertData. RunWorkerAsync (count );
}
Private void bgwInsertData_DoWork (object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
// 2. Call the custom function in DoWork and pass the sender that triggers the DoWork event
InsertData (worker );
}
Private void bgwInsertData_ProgressChanged (object sender, ProgressChangedEventArgs e)
{
ProgressBar1.Value = e. ProgressPercentage;
}
// Custom function insertData ()
Private void insertData (BackgroundWorker worker)
{
SqlConnection conn = new SqlConnection (@ "Data Source =. \ sqlexpress; Initial Catalog = bgwTestDB; Integrated Security = True ");
SqlCommand cmd = new SqlCommand ("insertOneData", conn );
Cmd. CommandType = CommandType. StoredProcedure;
Cmd. Parameters. Add ("data1", SqlDbType. NChar, 10 );
Cmd. Parameters. Add ("data2", SqlDbType. Int );
For (int I = 0; I <count; I ++)
{
Try
{
Conn. Open ();
Cmd. Parameters ["data1"]. Value = I + 1;
Cmd. Parameters ["data2"]. Value = I + 1;
Cmd. ExecuteNonQuery ();
// 3. Call the ReportProgress function of worker to trigger event ProgressChanged
Worker. ReportProgress (I, worker );
}
Catch (Exception ex)
{
MessageBox. Show (ex. Message );
}
Finally
{
If (conn. State = ConnectionState. Open)
Conn. Close ();
}
Thread. Sleep (50 );
}
}
Private void bgwInsertData_RunWorkerCompleted (object sender, RunWorkerCompletedEventArgs e)
{
If (e. Error! = Null)
{
MessageBox. Show (e. Error. Message, "prompt", MessageBoxButtons. OK, MessageBoxIcon. Error );
Return;
}
Else if (e. Cancelled)
{
MessageBox. Show ("cancel operation! "," Prompt ", MessageBoxButtons. OK, MessageBoxIcon. Information );
Return;
}
Else
MessageBox. Show ("operation successful! "," Prompt ", MessageBoxButtons. OK, MessageBoxIcon. Information );
}
}
}