C # BackGroundWorker Control

Source: Internet
Author: User

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 );
}
}
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.