Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. BackgroundWorker [closed]

BackgroundWorker [closed]

Scheduled Pinned Locked Moved .NET (Core and Framework)
questionannouncementlearning
10 Posts 2 Posters 24 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hshan_
    wrote on last edited by
    #1

    i'm trying to update label named "timer" with values from my prank function

    #pragma endregion
    void prank()
    {
    Beep(1500, 200);
    for(i=10;i>=0;i--)
    {
    Sleep(500);
    this->timer->Text =i+"";
    Beep(1500, 200);
    }
    }
    private: System::Void joke_Load(System::Object ^ sender, System::EventArgs ^ e) {
    TopMost = true;
    backgroundWorker1->RunWorkerAsync(1);
    }
    private: System::Void button1_Click(System::Object ^ sender, System::EventArgs ^ e) {
    joke^ nokno = gcnew joke();
    nokno->Show();
    //
    }
    private: System::Void label1_Click(System::Object ^ sender, System::EventArgs ^ e) {

    }
    public: System::Void backgroundWorker1\_DoWork(System::Object ^ sender, System::ComponentModel::DoWorkEventArgs ^ e) {
    	prank();
    }
    public: System::Void backgroundWorker1\_ProgressChanged(System::Object ^ sender, System::ComponentModel::DoWorkEventArgs ^ e)
    {
    	this->timer->Text = i + "";
    	Beep(1500, 200);
    }
    

    private: System::Void start_Click(System::Object^ sender, System::EventArgs^ e) {

    }

    it's not supposed to be real timer or anything i just want it to happen in the background so i can make another actions. the first Beep indicates for me that it starts but second one not really also i can't manage to use

    backgroundWorker1_ProgressChanged

    as you can see i have pitifully tried updating it ?traditional? way. i'm learning to code by myself (also english so sorry :^) ) and this is my first attempt to use background worker i could really use some suggestions and please keep them simple after all i'm simpleton?

    Richard DeemingR 1 Reply Last reply
    0
    • H hshan_

      i'm trying to update label named "timer" with values from my prank function

      #pragma endregion
      void prank()
      {
      Beep(1500, 200);
      for(i=10;i>=0;i--)
      {
      Sleep(500);
      this->timer->Text =i+"";
      Beep(1500, 200);
      }
      }
      private: System::Void joke_Load(System::Object ^ sender, System::EventArgs ^ e) {
      TopMost = true;
      backgroundWorker1->RunWorkerAsync(1);
      }
      private: System::Void button1_Click(System::Object ^ sender, System::EventArgs ^ e) {
      joke^ nokno = gcnew joke();
      nokno->Show();
      //
      }
      private: System::Void label1_Click(System::Object ^ sender, System::EventArgs ^ e) {

      }
      public: System::Void backgroundWorker1\_DoWork(System::Object ^ sender, System::ComponentModel::DoWorkEventArgs ^ e) {
      	prank();
      }
      public: System::Void backgroundWorker1\_ProgressChanged(System::Object ^ sender, System::ComponentModel::DoWorkEventArgs ^ e)
      {
      	this->timer->Text = i + "";
      	Beep(1500, 200);
      }
      

      private: System::Void start_Click(System::Object^ sender, System::EventArgs^ e) {

      }

      it's not supposed to be real timer or anything i just want it to happen in the background so i can make another actions. the first Beep indicates for me that it starts but second one not really also i can't manage to use

      backgroundWorker1_ProgressChanged

      as you can see i have pitifully tried updating it ?traditional? way. i'm learning to code by myself (also english so sorry :^) ) and this is my first attempt to use background worker i could really use some suggestions and please keep them simple after all i'm simpleton?

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      Your prank function is running on a background thread, and cannot directly update any UI controls. When you try to set the timer's Text property, an exception will be thrown, but since you're not handing the RunWorkerCompleted event[^] to inspect the result, you never see it. You need to call the BackgroundWorker.ReportProgress Method (System.ComponentModel) | Microsoft Docs[^] method to raise the ProgressChanged event on the UI thread to update the controls.


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      H 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        Your prank function is running on a background thread, and cannot directly update any UI controls. When you try to set the timer's Text property, an exception will be thrown, but since you're not handing the RunWorkerCompleted event[^] to inspect the result, you never see it. You need to call the BackgroundWorker.ReportProgress Method (System.ComponentModel) | Microsoft Docs[^] method to raise the ProgressChanged event on the UI thread to update the controls.


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        H Offline
        H Offline
        hshan_
        wrote on last edited by
        #3

        public: System::Void joke_Load(System::Object^ sender, System::EventArgs^ e) {
        bgwork->RunWorkerAsync();
        }
        public: System::Void labb_Click(System::Object^ sender, System::EventArgs^ e) {
        Beep(2500, 100);//label beeping
        }
        public: System::Void bgwork_DoWork(System::Object^ sender, System::ComponentModel::DoWorkEventArgs^ e)
        {

        for (i=10; i >=0; i--)
        	{
        		bgwork->ReportProgress(i);
        		Sleep(1000);
        		Beep(1500, 100);
        	}
        e->Result =i;
        }
        public: System::Void bgwork\_ProgressChanged(System::Object^ sender, System::ComponentModel::ProgressChangedEventArgs^ e)
        {
        	this->faketimer->Text =e->ProgressPercentage.ToString()+"";
        }
        public: System::Void bgwork\_RunWorkerCompleted(System::Object^ sender, System::ComponentModel::RunWorkerCompletedEventArgs^ e)
        {
        	MessageBox::Show("ok", "ok", MessageBoxButtons::YesNo, MessageBoxIcon::Warning);
        
        }
        

        label "faketimer" still has default text this piece with converting to string is something i saw in tutorial i guess it will not work because my values are decrementing

        Richard DeemingR 1 Reply Last reply
        0
        • H hshan_

          public: System::Void joke_Load(System::Object^ sender, System::EventArgs^ e) {
          bgwork->RunWorkerAsync();
          }
          public: System::Void labb_Click(System::Object^ sender, System::EventArgs^ e) {
          Beep(2500, 100);//label beeping
          }
          public: System::Void bgwork_DoWork(System::Object^ sender, System::ComponentModel::DoWorkEventArgs^ e)
          {

          for (i=10; i >=0; i--)
          	{
          		bgwork->ReportProgress(i);
          		Sleep(1000);
          		Beep(1500, 100);
          	}
          e->Result =i;
          }
          public: System::Void bgwork\_ProgressChanged(System::Object^ sender, System::ComponentModel::ProgressChangedEventArgs^ e)
          {
          	this->faketimer->Text =e->ProgressPercentage.ToString()+"";
          }
          public: System::Void bgwork\_RunWorkerCompleted(System::Object^ sender, System::ComponentModel::RunWorkerCompletedEventArgs^ e)
          {
          	MessageBox::Show("ok", "ok", MessageBoxButtons::YesNo, MessageBoxIcon::Warning);
          
          }
          

          label "faketimer" still has default text this piece with converting to string is something i saw in tutorial i guess it will not work because my values are decrementing

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #4

          Did you make sure you set the BackgroundWorker.WorkerReportsProgress property[^] to true?


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          H 2 Replies Last reply
          0
          • Richard DeemingR Richard Deeming

            Did you make sure you set the BackgroundWorker.WorkerReportsProgress property[^] to true?


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            H Offline
            H Offline
            hshan_
            wrote on last edited by
            #5

            yes i did :^) , only left workerSupportsCancel to false because i don't really need this. I have abandoned that project couple days ago and now with another one i still have problem with the same thing

            private: System::Void backgroundWorker1_DoWork(System::Object^ sender, System::ComponentModel::DoWorkEventArgs^ e) {

            	for (int day = 1; day <= 31; day++)
            	{
            		Beep(1500, 200);
            		Sleep(500);
            		backgroundWorker1->ReportProgress(day);
            
            	}
            

            }
            private: System::Void backgroundWorker1_ProgressChanged(System::Object^ sender, System::ComponentModel::ProgressChangedEventArgs^ e) {
            progressBar1->Value += e->ProgressPercentage;
            this->days->Text = (e->ProgressPercentage.ToString() + " day");
            }
            private: System::Void backgroundWorker1_RunWorkerCompleted(System::Object^ sender, System::ComponentModel::RunWorkerCompletedEventArgs^ e)
            {
            MessageBox::Show("next");
            }

            My backgroundWorker1->RunWorkerAsync(); is in form_load so it should start when i open it and still no success with this m*th*r******* label update. There is also no beeping indicating this shit even works

            Quote:

            I think i'm going to quit this shit start farming, somewhere nice with no internet access

            Richard DeemingR 1 Reply Last reply
            0
            • Richard DeemingR Richard Deeming

              Did you make sure you set the BackgroundWorker.WorkerReportsProgress property[^] to true?


              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

              H Offline
              H Offline
              hshan_
              wrote on last edited by
              #6

              private: System::Void start_Click(System::Object^ sender, System::EventArgs^ e) {
              backgroundWorker1->RunWorkerAsync(1);
              this->start->Visible = false;
              }
              private: System::Void backgroundWorker1_DoWork(System::Object^ sender, System::ComponentModel::DoWorkEventArgs^ e) {

              while (true)
              {
              	if (backgroundWorker1->CancellationPending) //if it was cancelled
              	{
              		e->Cancel = true;
              		
              	}
              	if (progressBar1->Value == progressBar1->Maximum)  //if the progress bar value reached maximum
              	{
              		
              	}
              	Beep(1500, 100);
              	backgroundWorker1->ReportProgress(50);  //reporting progress
              	Sleep(500);   //wait for 1 second
              }
              

              }

              private: System::Void cancel_Click(System::Object^ sender, System::EventArgs^ e) {
              backgroundWorker1->CancelAsync();
              this->start->Visible = true;
              }
              private: System::Void backgroundWorker1_ProgressChanged(System::Object^ sender, System::ComponentModel::ProgressChangedEventArgs^ e) {

              	   progressBar1->Value += e->ProgressPercentage;  //rising the progressbar's value
              
                 }
              

              private: System::Void backgroundWorker1_RunWorkerCompleted(System::Object^ sender, System::ComponentModel::RunWorkerCompletedEventArgs^ e) {
              this->start->Visible = true;
              progressBar1->Value = 0; //reseting value

              if (e->Cancelled)    //Messages for the events
              {
              	MessageBox::Show("kiss my ass ");
              }
              else
              {
              	MessageBox::Show("eat my shit ");
              }
              

              }
              };
              }

              ^ | | this shit above is from Add new comment | Free Source Code[^] where i found one and only tutorial how to use background worker in c++ an not in c# to this moment i was only using some formulas from it but now i coppied everything AND..... it's not working progress bar is not updating i wasn't even sure if it works untill i added beep and it works but not quite not updating this fucking progress bar cancel button doesn't work (but it kinda does) i'm really on the edge right now im this >< close to lose my shit

              1 Reply Last reply
              0
              • H hshan_

                yes i did :^) , only left workerSupportsCancel to false because i don't really need this. I have abandoned that project couple days ago and now with another one i still have problem with the same thing

                private: System::Void backgroundWorker1_DoWork(System::Object^ sender, System::ComponentModel::DoWorkEventArgs^ e) {

                	for (int day = 1; day <= 31; day++)
                	{
                		Beep(1500, 200);
                		Sleep(500);
                		backgroundWorker1->ReportProgress(day);
                
                	}
                

                }
                private: System::Void backgroundWorker1_ProgressChanged(System::Object^ sender, System::ComponentModel::ProgressChangedEventArgs^ e) {
                progressBar1->Value += e->ProgressPercentage;
                this->days->Text = (e->ProgressPercentage.ToString() + " day");
                }
                private: System::Void backgroundWorker1_RunWorkerCompleted(System::Object^ sender, System::ComponentModel::RunWorkerCompletedEventArgs^ e)
                {
                MessageBox::Show("next");
                }

                My backgroundWorker1->RunWorkerAsync(); is in form_load so it should start when i open it and still no success with this m*th*r******* label update. There is also no beeping indicating this shit even works

                Quote:

                I think i'm going to quit this shit start farming, somewhere nice with no internet access

                Richard DeemingR Offline
                Richard DeemingR Offline
                Richard Deeming
                wrote on last edited by
                #7

                It sounds like you haven't wired up the event handlers. Check the generated InitializeComponent method to make sure the three events (DoWork, ProgressChanged, and RunWorkerCompleted) are connected to the relevant event handler methods.


                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                H 1 Reply Last reply
                0
                • Richard DeemingR Richard Deeming

                  It sounds like you haven't wired up the event handlers. Check the generated InitializeComponent method to make sure the three events (DoWork, ProgressChanged, and RunWorkerCompleted) are connected to the relevant event handler methods.


                  "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                  H Offline
                  H Offline
                  hshan_
                  wrote on last edited by
                  #8

                  what i didnt understand a thing you just wrote and i would really appreciate if you could simplify and maybe show me some example me big noob i started programming like 2 months ago for real and english is not my first language

                  Richard DeemingR 1 Reply Last reply
                  0
                  • H hshan_

                    what i didnt understand a thing you just wrote and i would really appreciate if you could simplify and maybe show me some example me big noob i started programming like 2 months ago for real and english is not my first language

                    Richard DeemingR Offline
                    Richard DeemingR Offline
                    Richard Deeming
                    wrote on last edited by
                    #9

                    I don't use C++, but there should be code similar to the Microsoft example:

                    BackgroundWorker Class (System.ComponentModel) | Microsoft Docs[^]:

                    backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
                    backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
                    backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);

                    I believe for C++/CLI, it would be something like:

                    backgroundWorker1->DoWork += gcnew DoWorkEventHandler(this, &backgroundWorker1_DoWork);

                    How to: Use Events in C++/CLI | Microsoft Docs[^]


                    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                    "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                    H 1 Reply Last reply
                    0
                    • Richard DeemingR Richard Deeming

                      I don't use C++, but there should be code similar to the Microsoft example:

                      BackgroundWorker Class (System.ComponentModel) | Microsoft Docs[^]:

                      backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
                      backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
                      backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);

                      I believe for C++/CLI, it would be something like:

                      backgroundWorker1->DoWork += gcnew DoWorkEventHandler(this, &backgroundWorker1_DoWork);

                      How to: Use Events in C++/CLI | Microsoft Docs[^]


                      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                      H Offline
                      H Offline
                      hshan_
                      wrote on last edited by
                      #10

                      ok everyting works this->backgroundWorker1->DoWork += gcnew System::ComponentModel::DoWorkEventHandler(this, &fuck::backgroundWorker1_DoWork); this->backgroundWorker1->ProgressChanged += gcnew System::ComponentModel::ProgressChangedEventHandler(this, &fuck::backgroundWorker1_ProgressChanged); this->backgroundWorker1->RunWorkerCompleted += gcnew System::ComponentModel::RunWorkerCompletedEventHandler(this, &fuck::backgroundWorker1_RunWorkerCompleted); was indeed missing

                      private: System::Void backgroundWorker1_DoWork(System::Object^ sender, System::ComponentModel::DoWorkEventArgs^ e) {

                      while (true)
                      {
                      	if (backgroundWorker1->CancellationPending) //if it was cancelled
                      	{
                      		e->Cancel = true;
                      		
                      	}
                      	if (progressBar1->Value == progressBar1->Maximum)  //if the progress bar value reached maximum
                      

                      Valuse==progressBar1->Maximum does not stop DoWork this "Maximum"

                      	{
                      		
                      	}
                      	Beep(1500, 100);
                      	backgroundWorker1->ReportProgress(50);  //reporting progress
                      	Sleep(500);   //wait for 1 second
                      }
                      

                      }

                      now i really got everything i needed thanks for help

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups