dear all
did anyone know how to implement backgroundworker tools using C# language.
Thank You
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
jingeRank #2,527 · 554 reputationPosted Nov 2, 2009, 3:51 PM EST
http://dotnetperls.com/backgroundworker
Posted Nov 2, 2009, 4:58 PM EST
http://weblogs.asp.net/rosherove/articles/BackgroundWorker.aspx
with regards
prasad
Kirtan PatelRank #292 · 8.6K reputationPosted Nov 2, 2009, 5:28 PM EST
Just Drag One Back Ground Worker tool on Form
then In Do_Work Event Write the Code you want to Run Separately
and In Form_Load Event Just Execute RunWorkerAysnc() method to start Execution of Background Worker :)
private void Form1_Load(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
//Write the Code Here Which you want to Run in Seperate Thread :)
}
If my answer helps you then check "Do you like this answer" :)
Nilanka DharmadasaRank #1,875 · 794 reputationPosted Nov 2, 2009, 6:57 PM EST
Backgroundworker component allows you to create a thread and execute a seperate process on that thread. During the process, backgroundworker can notify the progress to the main thread. And also user can abort this process at any time.
First go to design mode and drag a backgroundworker to the form. Then set the following properties of the backgroundworker.
First you must bind the following event of backgroundworker.
If you want to allow the user to abort the thread, set following property.
And also bind the following event of backgroundworker if you have something to do after the thread is finished.
Then if you want it to report the progress, set following property.
And also bind the following event of backgroundworker.
private void btnStartThread_Click(object sender, EventArgs e) { backgroundWorker1.RunWorkerAsync(); //By calling this, the process is started and it fires backgroundWorker1_DoWork event. } private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { for (int i = 0; i //By this method you can report the progress. 'i' is the percentage of the completed work. backgroundWorker1.ReportProgress(i); Thread.Sleep(100); if (backgroundWorker1.CancellationPending) { //If the user cancels the process this part of the code will be executed. e.Cancel = true; return; } } } private void btnCancelThread_Click(object sender, EventArgs e) { //When the user wants to cancel, you should call this method. This will set 'CancellationPending' to true. backgroundWorker1.CancelAsync(); } /*This method is called once the process is finished. You can check the reason for the process to be finished. Also you can use this method to don anything that you wich to do after the thread is completed.*/ private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e){ if(e.Cancelled) Console.WriteLine("User Cancelled."); else if(e.Error) Console.WriteLine("Error occured."); else Console.WriteLine("Process Completed."); } private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) { /*Here, you can access the progress you set at the 'Do_work' event.I have used that value to update a progress bar. */ progressBar1.Value = e.ProgressPercentage; }Hope this will help you. If it helps you please accept it.