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. BackgroundWorker method executing twice

BackgroundWorker method executing twice

Scheduled Pinned Locked Moved C#
helptutorial
6 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
    scody
    wrote on last edited by
    #1

    Below is the skeleton code which demonstrates the way I have used the BackgroundWorker class. I followed the example provided in MSDN. For some reason MyTimeConsumingTask executes twice before the bgWorker_RunWorkerCompleted handles it. Any help would be greatly appriciated.

        //Form Class
        void bgWorker\_DoWork(object sender, DoWorkEventArgs e)
        {
            // Get the BackgroundWorker that raised this event.
            BackgroundWorker worker = sender as BackgroundWorker;
    
            e.Result = MyTimeConsumingTask(e.Argument, worker, e);
    
        }
    
        void bgWorker\_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            // First, handle the case where an exception was thrown.
            if (e.Error != null)
            {
                MessageBox.Show(e.Error.Message);
            }
            else if ((bool)e.Result)
            {
                //MyTimeConsumingTask successfully executed
            }
            else
            {
                //MyTimeConsumingTask failed
            }
        }
        //Form Class end
    
    
        //Another Class
        internal bool MyTimeConsumingTask(object arg, BackgroundWorker worker, DoWorkEventArgs e)
        {
            //Actual task is performed here, for simplicity sleep has been used.
            Thread.Sleep(10000);
            return true;
        }
        //Another Class end
    
    N Steve EcholsS 2 Replies Last reply
    0
    • S scody

      Below is the skeleton code which demonstrates the way I have used the BackgroundWorker class. I followed the example provided in MSDN. For some reason MyTimeConsumingTask executes twice before the bgWorker_RunWorkerCompleted handles it. Any help would be greatly appriciated.

          //Form Class
          void bgWorker\_DoWork(object sender, DoWorkEventArgs e)
          {
              // Get the BackgroundWorker that raised this event.
              BackgroundWorker worker = sender as BackgroundWorker;
      
              e.Result = MyTimeConsumingTask(e.Argument, worker, e);
      
          }
      
          void bgWorker\_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
          {
              // First, handle the case where an exception was thrown.
              if (e.Error != null)
              {
                  MessageBox.Show(e.Error.Message);
              }
              else if ((bool)e.Result)
              {
                  //MyTimeConsumingTask successfully executed
              }
              else
              {
                  //MyTimeConsumingTask failed
              }
          }
          //Form Class end
      
      
          //Another Class
          internal bool MyTimeConsumingTask(object arg, BackgroundWorker worker, DoWorkEventArgs e)
          {
              //Actual task is performed here, for simplicity sleep has been used.
              Thread.Sleep(10000);
              return true;
          }
          //Another Class end
      
      N Offline
      N Offline
      N a v a n e e t h
      wrote on last edited by
      #2

      Put break points and step into the code to see what is wrong.

      All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions

      S 1 Reply Last reply
      0
      • S scody

        Below is the skeleton code which demonstrates the way I have used the BackgroundWorker class. I followed the example provided in MSDN. For some reason MyTimeConsumingTask executes twice before the bgWorker_RunWorkerCompleted handles it. Any help would be greatly appriciated.

            //Form Class
            void bgWorker\_DoWork(object sender, DoWorkEventArgs e)
            {
                // Get the BackgroundWorker that raised this event.
                BackgroundWorker worker = sender as BackgroundWorker;
        
                e.Result = MyTimeConsumingTask(e.Argument, worker, e);
        
            }
        
            void bgWorker\_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
            {
                // First, handle the case where an exception was thrown.
                if (e.Error != null)
                {
                    MessageBox.Show(e.Error.Message);
                }
                else if ((bool)e.Result)
                {
                    //MyTimeConsumingTask successfully executed
                }
                else
                {
                    //MyTimeConsumingTask failed
                }
            }
            //Form Class end
        
        
            //Another Class
            internal bool MyTimeConsumingTask(object arg, BackgroundWorker worker, DoWorkEventArgs e)
            {
                //Actual task is performed here, for simplicity sleep has been used.
                Thread.Sleep(10000);
                return true;
            }
            //Another Class end
        
        Steve EcholsS Offline
        Steve EcholsS Offline
        Steve Echols
        wrote on last edited by
        #3

        You're not accidentally adding another handler to DoWork are you? bgWorker.DoWork += new DoWorkEventHandler(bgWorker_DoWork); bgWorker.DoWork += new DoWorkEventHandler(bgWorker_DoWork); If you do that, it will be called twice.


        - S 50 cups of coffee and you know it's on! A post a day, keeps the white coats away!

        • S
          50 cups of coffee and you know it's on!
          Code, follow, or get out of the way.
        S 1 Reply Last reply
        0
        • N N a v a n e e t h

          Put break points and step into the code to see what is wrong.

          All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions

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

          Checked with break points at every line of code involving this event. Although the e.result of first execution is true, the bgWorker_RunWorkerCompleted doesn't handle it but handles it after second execution. Below is the sequence of the code execution as traced by break points, strange but true...:confused:

          BackgroundWorker worker = sender as BackgroundWorker; //bgWorker_DoWork handler
          e.Result = MyTimeConsumingTask(e.Argument, worker, e); //bgWorker_DoWork handler

          Thread.Sleep(10000); //MyTimeConsumingTask method
          return true; //MyTimeConsumingTask mehtod

          BackgroundWorker worker = sender as BackgroundWorker; //bgWorker_DoWork handler
          e.Result = MyTimeConsumingTask(e.Argument, worker, e); //bgWorker_DoWork handler

          Thread.Sleep(10000); //MyTimeConsumingTask method
          return true; //MyTimeConsumingTask method

          else if ((bool)e.Result) //bgWorker_RunWorkerCompleted handler

          realJSOPR 1 Reply Last reply
          0
          • Steve EcholsS Steve Echols

            You're not accidentally adding another handler to DoWork are you? bgWorker.DoWork += new DoWorkEventHandler(bgWorker_DoWork); bgWorker.DoWork += new DoWorkEventHandler(bgWorker_DoWork); If you do that, it will be called twice.


            - S 50 cups of coffee and you know it's on! A post a day, keeps the white coats away!

            S Offline
            S Offline
            scody
            wrote on last edited by
            #5

            Thanks Steve! You were right, I checked the whole project for DoWork and found that the Visual Studio had already added the DoWorkEventHandler in its generated code InitializeComponent() Now after removing it, works as expected. :-D

            1 Reply Last reply
            0
            • S scody

              Checked with break points at every line of code involving this event. Although the e.result of first execution is true, the bgWorker_RunWorkerCompleted doesn't handle it but handles it after second execution. Below is the sequence of the code execution as traced by break points, strange but true...:confused:

              BackgroundWorker worker = sender as BackgroundWorker; //bgWorker_DoWork handler
              e.Result = MyTimeConsumingTask(e.Argument, worker, e); //bgWorker_DoWork handler

              Thread.Sleep(10000); //MyTimeConsumingTask method
              return true; //MyTimeConsumingTask mehtod

              BackgroundWorker worker = sender as BackgroundWorker; //bgWorker_DoWork handler
              e.Result = MyTimeConsumingTask(e.Argument, worker, e); //bgWorker_DoWork handler

              Thread.Sleep(10000); //MyTimeConsumingTask method
              return true; //MyTimeConsumingTask method

              else if ((bool)e.Result) //bgWorker_RunWorkerCompleted handler

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

              Try using trace statements instead of watching it through the debugger, and put a breakpoint at the end of the OnCompleted method. When the breakpoint is hit, examine the trace statements.

              "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

              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