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. C#
  4. Background worker thread

Background worker thread

Scheduled Pinned Locked Moved C#
hardwarehelp
11 Posts 4 Posters 0 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.
  • S Offline
    S Offline
    Saamir
    wrote on last edited by
    #1

    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

    L L 2 Replies Last reply
    0
    • S Saamir

      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

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      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:


      S 1 Reply Last reply
      0
      • S Saamir

        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

        L Offline
        L Offline
        Lev Danielyan
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • L Luc Pattyn

          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:


          S Offline
          S Offline
          Saamir
          wrote on last edited by
          #4

          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;

          L realJSOPR 2 Replies Last reply
          0
          • S Saamir

            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;

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            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:


            S 1 Reply Last reply
            0
            • S Saamir

              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;

              realJSOPR Offline
              realJSOPR Offline
              realJSOP
              wrote on last edited by
              #6

              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

              S 1 Reply Last reply
              0
              • realJSOPR realJSOP

                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

                S Offline
                S Offline
                Saamir
                wrote on last edited by
                #7

                Thank you John for your comment, you didn't have to curse to make a point.

                Sameer

                realJSOPR 1 Reply Last reply
                0
                • S Saamir

                  Thank you John for your comment, you didn't have to curse to make a point.

                  Sameer

                  realJSOPR Offline
                  realJSOPR Offline
                  realJSOP
                  wrote on last edited by
                  #8

                  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

                  S 1 Reply Last reply
                  0
                  • L Luc Pattyn

                    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:


                    S Offline
                    S Offline
                    Saamir
                    wrote on last edited by
                    #9

                    Thank you Luc, the link you sent is pretty good and I appreciate your comments.

                    Sameer

                    L 1 Reply Last reply
                    0
                    • realJSOPR realJSOP

                      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

                      S Offline
                      S Offline
                      Saamir
                      wrote on last edited by
                      #10

                      Thank you and God bless you

                      Sameer

                      1 Reply Last reply
                      0
                      • S Saamir

                        Thank you Luc, the link you sent is pretty good and I appreciate your comments.

                        Sameer

                        L Offline
                        L Offline
                        Luc Pattyn
                        wrote on last edited by
                        #11

                        You're welcome. :)

                        Luc Pattyn [Forum Guidelines] [My Articles]


                        Fixturized forever. :confused:


                        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