BackgroundWorker [closed]
-
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?
-
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?
Your
prank
function is running on a background thread, and cannot directly update any UI controls. When you try to set the timer'sText
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 theProgressChanged
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
-
Your
prank
function is running on a background thread, and cannot directly update any UI controls. When you try to set the timer'sText
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 theProgressChanged
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
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
-
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
Did you make sure you set the
BackgroundWorker.WorkerReportsProgress
property[^] totrue
?
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Did you make sure you set the
BackgroundWorker.WorkerReportsProgress
property[^] totrue
?
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
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
-
Did you make sure you set the
BackgroundWorker.WorkerReportsProgress
property[^] totrue
?
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
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 valueif (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
-
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
It sounds like you haven't wired up the event handlers. Check the generated
InitializeComponent
method to make sure the three events (DoWork
,ProgressChanged
, andRunWorkerCompleted
) 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
-
It sounds like you haven't wired up the event handlers. Check the generated
InitializeComponent
method to make sure the three events (DoWork
,ProgressChanged
, andRunWorkerCompleted
) 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
-
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
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
-
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
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