Background worker thread
-
Hi guys, I am a little new to threading and I need your help please. I am trying to use a background worker thread and can't get it to work: Here is a little more explanation on my code I have a button click where I call a function which has a while loop waiting for a status from a piece of hardware. In my button click event I am calling this.bgwSign.RunWorkerAsync(); and then I wait for the status. In my bgwSign_DoWork(), I call the while loop which waits for the status from the hardware In my while loop I have this.bgwSign.ReportProgress(SignatureStatus, timer1.Interval); I don't think this is correct Please helpppp!!!
Sameer
-
Hi guys, I am a little new to threading and I need your help please. I am trying to use a background worker thread and can't get it to work: Here is a little more explanation on my code I have a button click where I call a function which has a while loop waiting for a status from a piece of hardware. In my button click event I am calling this.bgwSign.RunWorkerAsync(); and then I wait for the status. In my bgwSign_DoWork(), I call the while loop which waits for the status from the hardware In my while loop I have this.bgwSign.ReportProgress(SignatureStatus, timer1.Interval); I don't think this is correct Please helpppp!!!
Sameer
Hi, it is hard to provide detailed help without seeing actual code. anyway, having a handler (button click or anything else) wait for something that could take hundreds of milliseconds or more, is not a good approach; for one it blocks the entire GUI, and it also defeats the very purpose of using a background worker. If you need one, here is a good introduction to BGWs[^]. Hope this helps.
Luc Pattyn [Forum Guidelines] [My Articles]
Fixturized forever. :confused:
-
Hi guys, I am a little new to threading and I need your help please. I am trying to use a background worker thread and can't get it to work: Here is a little more explanation on my code I have a button click where I call a function which has a while loop waiting for a status from a piece of hardware. In my button click event I am calling this.bgwSign.RunWorkerAsync(); and then I wait for the status. In my bgwSign_DoWork(), I call the while loop which waits for the status from the hardware In my while loop I have this.bgwSign.ReportProgress(SignatureStatus, timer1.Interval); I don't think this is correct Please helpppp!!!
Sameer
Do you mean that ReportProgress doesn't work? If yes, have you set WorkerReportsProgress = true? I'm sure posting the code will help a lot.
Regards, Lev
-
Hi, it is hard to provide detailed help without seeing actual code. anyway, having a handler (button click or anything else) wait for something that could take hundreds of milliseconds or more, is not a good approach; for one it blocks the entire GUI, and it also defeats the very purpose of using a background worker. If you need one, here is a good introduction to BGWs[^]. Hope this helps.
Luc Pattyn [Forum Guidelines] [My Articles]
Fixturized forever. :confused:
Here is my code. What I am trying to do is to allow the GUI not to be locked while waiting for the status from the signature capture unit. private void cmdSign_Click(object sender, System.EventArgs e) { this.bgwSign.RunWorkerAsync(); if (SigStatus == 1) { cmdMgr.Enabled = true; cmdSign.Enabled = true; cmdCancel.Enabled = true; cmdSign.Focus(); //Application.DoEvents(); } else if (posSignature.GetVAR("btstate") == "btnCANCEL") { cmdMgr.Enabled = true; cmdSign.Enabled = true; cmdCancel.Enabled = true; cmdSign.Focus(); SigStatus = 1; } else { cmdCancel.Enabled = true; cmdMgr.Enabled = true; cmdSign.Enabled = true; cmdSign.Focus(); SigStatus = 1; } } private void bgwSign_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) { timer1.Interval = 60000; timer2.Enabled = false; timer2.Stop(); lblInstruction.Text = "Ask customer to sign the signature pad"; //this.Cursor = Cursors.WaitCursor; cmdNext.Enabled = false; cmdSign.Enabled = false; cmdCancel.Enabled = false; cmdMgr.Enabled = false; posSignImage = null; PboxSignature.Image = null; try { //Application.DoEvents(); timer1.Enabled = true; timer1.Start(); while (posSignature.GetVAR("btstate") != "btnENTER" || posSignature.GetVAR("btstate") != "btnCANCEL" || SigStatus == 0) { if (this.bgwSign.CancellationPending) { e.Cancel = true; return; } this.bgwSign.ReportProgress(SigStatus, timer1.Interval); System.Threading.Thread.Sleep(20); if (posSignature.GetVAR("btstate") == "btnCANCEL" || posSignature.GetVAR("btstate") == "btnENTER" || SigStatus == 1) { timer1.Enabled = false; timer1.Stop(); break;
-
Here is my code. What I am trying to do is to allow the GUI not to be locked while waiting for the status from the signature capture unit. private void cmdSign_Click(object sender, System.EventArgs e) { this.bgwSign.RunWorkerAsync(); if (SigStatus == 1) { cmdMgr.Enabled = true; cmdSign.Enabled = true; cmdCancel.Enabled = true; cmdSign.Focus(); //Application.DoEvents(); } else if (posSignature.GetVAR("btstate") == "btnCANCEL") { cmdMgr.Enabled = true; cmdSign.Enabled = true; cmdCancel.Enabled = true; cmdSign.Focus(); SigStatus = 1; } else { cmdCancel.Enabled = true; cmdMgr.Enabled = true; cmdSign.Enabled = true; cmdSign.Focus(); SigStatus = 1; } } private void bgwSign_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) { timer1.Interval = 60000; timer2.Enabled = false; timer2.Stop(); lblInstruction.Text = "Ask customer to sign the signature pad"; //this.Cursor = Cursors.WaitCursor; cmdNext.Enabled = false; cmdSign.Enabled = false; cmdCancel.Enabled = false; cmdMgr.Enabled = false; posSignImage = null; PboxSignature.Image = null; try { //Application.DoEvents(); timer1.Enabled = true; timer1.Start(); while (posSignature.GetVAR("btstate") != "btnENTER" || posSignature.GetVAR("btstate") != "btnCANCEL" || SigStatus == 0) { if (this.bgwSign.CancellationPending) { e.Cancel = true; return; } this.bgwSign.ReportProgress(SigStatus, timer1.Interval); System.Threading.Thread.Sleep(20); if (posSignature.GetVAR("btstate") == "btnCANCEL" || posSignature.GetVAR("btstate") == "btnENTER" || SigStatus == 1) { timer1.Enabled = false; timer1.Stop(); break;
Hi, that is almost impossible to read, lacking the declarations of lots of variables, and not using proper formatting (with PRE tags). The initialization of bgwSign is crucial for it to work properly; I doubt you have it all there, so I don't think your bgwSign_DoWork even started to run. Did you check? Also bgwSign_DoWork is not allowed to touch any GUI stuff; I guess you are violating that rule a dozen times with timer1, timer2, cmdNext, etc. You must refrain from touching Forms and Controls in any thread (or BGW) other than the main aka GUI thread. If the GUI needs updating, either use the Control.InvokeRequired/Control.Invoke pattern, or put it in the Progress/Completed handlers of the BGW (those are guaranteed to run on the GUI thread). I suggest you read up on this. :)
Luc Pattyn [Forum Guidelines] [My Articles]
Fixturized forever. :confused:
-
Here is my code. What I am trying to do is to allow the GUI not to be locked while waiting for the status from the signature capture unit. private void cmdSign_Click(object sender, System.EventArgs e) { this.bgwSign.RunWorkerAsync(); if (SigStatus == 1) { cmdMgr.Enabled = true; cmdSign.Enabled = true; cmdCancel.Enabled = true; cmdSign.Focus(); //Application.DoEvents(); } else if (posSignature.GetVAR("btstate") == "btnCANCEL") { cmdMgr.Enabled = true; cmdSign.Enabled = true; cmdCancel.Enabled = true; cmdSign.Focus(); SigStatus = 1; } else { cmdCancel.Enabled = true; cmdMgr.Enabled = true; cmdSign.Enabled = true; cmdSign.Focus(); SigStatus = 1; } } private void bgwSign_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) { timer1.Interval = 60000; timer2.Enabled = false; timer2.Stop(); lblInstruction.Text = "Ask customer to sign the signature pad"; //this.Cursor = Cursors.WaitCursor; cmdNext.Enabled = false; cmdSign.Enabled = false; cmdCancel.Enabled = false; cmdMgr.Enabled = false; posSignImage = null; PboxSignature.Image = null; try { //Application.DoEvents(); timer1.Enabled = true; timer1.Start(); while (posSignature.GetVAR("btstate") != "btnENTER" || posSignature.GetVAR("btstate") != "btnCANCEL" || SigStatus == 0) { if (this.bgwSign.CancellationPending) { e.Cancel = true; return; } this.bgwSign.ReportProgress(SigStatus, timer1.Interval); System.Threading.Thread.Sleep(20); if (posSignature.GetVAR("btstate") == "btnCANCEL" || posSignature.GetVAR("btstate") == "btnENTER" || SigStatus == 1) { timer1.Enabled = false; timer1.Stop(); break;
Well, we don't know if you've properly configured the background worker, you didn't show us your
ReportProgress
delegate, and the the greater sin is that you didn't put your code in a pre block. Pay-f*cking-attention to what *you're* doing so we can help you."Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001 -
Well, we don't know if you've properly configured the background worker, you didn't show us your
ReportProgress
delegate, and the the greater sin is that you didn't put your code in a pre block. Pay-f*cking-attention to what *you're* doing so we can help you."Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001 -
Yeah - I really did.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001 -
Hi, that is almost impossible to read, lacking the declarations of lots of variables, and not using proper formatting (with PRE tags). The initialization of bgwSign is crucial for it to work properly; I doubt you have it all there, so I don't think your bgwSign_DoWork even started to run. Did you check? Also bgwSign_DoWork is not allowed to touch any GUI stuff; I guess you are violating that rule a dozen times with timer1, timer2, cmdNext, etc. You must refrain from touching Forms and Controls in any thread (or BGW) other than the main aka GUI thread. If the GUI needs updating, either use the Control.InvokeRequired/Control.Invoke pattern, or put it in the Progress/Completed handlers of the BGW (those are guaranteed to run on the GUI thread). I suggest you read up on this. :)
Luc Pattyn [Forum Guidelines] [My Articles]
Fixturized forever. :confused:
-
Yeah - I really did.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001 -