C++ Tutorial: Using of BackgroundWorker Component in C++/CLI

Intro     Hi. It is Brigh777 and today I will show you how to use such component as backgroundworker in your C++ application. First of all, I tell you why we need, such component, as backgroundworker. Backgroundworker is a really good thing then you want to run some background processes in your application. The standard application does all calculations, commands and other things in one thread. It causes a lot of problems, such as a long time of work, the main window of your program can hangs , while you program will do calculations. Another reason of doing that is to make some background process (like “demons” in Linux), which will work as long as the program does.    So let’s get started. By the way, I am explaining this topic on MS Visual Studio 2012. So, don’t panic :) Creating
  1. First of all, you need to make a new C++ Windows Forms project. When you have done it, place the controls like in the window below: Window
  2. Then you need to choose a backgroundworker component from the list of tools and simply drag it on your form.
  3. After that modify the event of “Start” button by double clicking on it and writing the code below.
    1.         private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
    2.  
    3.                                  backgroundWorker1->RunWorkerAsync(1);  //starting background worker
    4.                          }
  4. The same for the “Cancel” button:
    1.         private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
    2.  
    3.                                  backgroundWorker1->CancelAsync();   //cancel backgroundworker
    4.                          }
  5. Then it is done, you need to review the settings of backgroundwarker component.
Modifying Properties tab Your properties should be like that: Capture
Modifying Events tab:
  1. Double click near the event DoWork. It will do a new function. Write in it the code below:
    1. private: System::Void backgroundWorker1_DoWork(System::Object^  sender, System::ComponentModel::DoWorkEventArgs^  e) {
    2.  
    3.                                 while(true)
    4.                                 {
    5.                                  if(backgroundWorker1->CancellationPending) //if it was cancelled
    6.                                         {
    7.                                                 e->Cancel=true;
    8.                                                 break;
    9.                                         }
    10.                                  if(progressBar1->Value==progressBar1->Maximum)  //if the progress bar value reached maximum
    11.                                  {
    12.                                          break;
    13.                                  }
    14.        
    15.                                  backgroundWorker1->ReportProgress(10);  //reporting progress
    16.                                  Thread::Sleep(1000);   //wait for 1 second
    17.                                 }
    18.                          }
    To use Thread::Sleep() you need to add System::Threading namespace to your program.
  2. 2) Modify ProgressChanged event in the same they:
    1. private: System::Void backgroundWorker1_ProgressChanged(System::Object^  sender, System::ComponentModel::ProgressChangedEventArgs^  e) {
    2.  
    3.                          progressBar1->Value+=e->ProgressPercentage;  //rising the progressbar's value
    4.  
    5.                  }
  3. And the last RunWorker complete event:
    1. private: System::Void backgroundWorker1_RunWorkerCompleted(System::Object^  sender, System::ComponentModel::RunWorkerCompletedEventArgs^  e) {
    2.  
    3.                          progressBar1->Value=0;   //reseting value
    4.  
    5.                          if(e->Cancelled)    //Messages for the events
    6.                          {
    7.                                  MessageBox::Show("You have cancelled background worker!!!");
    8.                          }
    9.                          else
    10.                          {
    11.                                  MessageBox::Show("Work completed!!");
    12.                          }
    13.                  }
Results:
  1. Running program: Window
  2. Message when work is done: Window
  3. Message when work is cancelled: Window

Comments

Thanks for this tutorial, I'd read about 10 others before this which were making absolutely no sense to me, but after following along with yours i was able to fully understand and apply to my program. Thanks you so much!

Thank you for this tutorial it has helped me a lot.

not working how you explain that? everything done exactly like shown above and no success fuck this shit i about to commit die

it seems encouraging but I get errors in bcrypt.h and visual 17 says I made changes to this file. typical, I keep researching.

Add new comment