locked
C# BackGroundWorker RRS feed

  • Question

  • i am using a backgroundworker class to run an method asynchronously which takes more time to process.

    BackgroundWorker backgndWkr = new BackgroundWorker();
                                        backgndWkr.DoWork += new DoWorkEventHandler(backgndWkr_DoWork); // takes more time to process
                                        backgndWkr.RunWorkerAsync("Test");
    void backgndWkr_DoWork(object sender, DoWorkEventArgs e)
            {
                Thread.Sleep(100000);
            }

    Now how can i dispose the backgroundworker object ?

    Tuesday, September 14, 2010 12:07 PM

Answers

  • Hi,

    I've dabbled with the code a little and this is what I came up with. If you loose the scope to your BackgroundWorker instance you can still dispose it in the RunWorkerCompleted event. Here is a code sample:

    void backgndWkr_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
     IDisposable disposer = sender as IDisposable;
     if (disposer != null)
     {
      disposer.Dispose();
     }
    }
    

    And if you want to be sure your variable is also disposed completely you may also hook up the Disposed event like so:

    BackgroundWorker backgndWkr = new BackgroundWorker();
    backgndWkr.DoWork += new DoWorkEventHandler(backgndWkr_DoWork);
    backgndWkr.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgndWkr_RunWorkerCompleted);
    backgndWkr.Disposed += (o, e) =>
    {
     backgndWkr = null;
    };
    backgndWkr.RunWorkerAsync("Test");
    
    

    Hope this helps
    Meile Zetstra

    Tuesday, September 14, 2010 1:51 PM

All replies

  • I am not clear about your question but to answer "how can i dispose the backgroundworker object ?"

    you can call dispose method of backgroundworker object. Something like this will work backgndWkr.Dispose().

     


    Abhinav Galodha
    Tuesday, September 14, 2010 1:23 PM
  • Hi,

    I've dabbled with the code a little and this is what I came up with. If you loose the scope to your BackgroundWorker instance you can still dispose it in the RunWorkerCompleted event. Here is a code sample:

    void backgndWkr_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
     IDisposable disposer = sender as IDisposable;
     if (disposer != null)
     {
      disposer.Dispose();
     }
    }
    

    And if you want to be sure your variable is also disposed completely you may also hook up the Disposed event like so:

    BackgroundWorker backgndWkr = new BackgroundWorker();
    backgndWkr.DoWork += new DoWorkEventHandler(backgndWkr_DoWork);
    backgndWkr.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgndWkr_RunWorkerCompleted);
    backgndWkr.Disposed += (o, e) =>
    {
     backgndWkr = null;
    };
    backgndWkr.RunWorkerAsync("Test");
    
    

    Hope this helps
    Meile Zetstra

    Tuesday, September 14, 2010 1:51 PM
  • in the above code snippet, i was asking about how to dispose the "backgndWkr" object?

     

    Wednesday, September 15, 2010 4:19 AM
  • in the above code snippet, i was asking about how to dispose the "backgndWkr" object?

    As Meile Zetstra said:  "you can dispose it in the RunWorkerCompleted event."
    Wednesday, September 15, 2010 5:12 AM
  • You can find a detailed explanation here also.

    www.midnightprogramer.net
    Wednesday, September 15, 2010 9:30 AM