locked
Do I need to dispose a BackgroundWorker object that never completes? RRS feed

  • Question

  • User314655009 posted

    Hello,

    I have a background worker in an ASP.NET Web application that runs in a loop.

    Do I need to Dispose the object on Page closing?

    williamj

    Friday, December 16, 2016 3:05 PM

Answers

  • User765422875 posted

    Is the class you are using?

    https://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker(v=vs.110).aspx

    If it is, then no you do not need to dispose.

    BackgroundWorker derives from Component. Component implements the IDisposable interface. This makes BackgroundWorker inherit the Dispose() method. A Backgroundworker is a relatively 'light' object, it holds no resources. The thread is borrowed from the ThreadPool.

    If you are using ASP.NET and you want to run a background task, then at a minimum you should use HostingEnvironment.QueueBackgroundWorkItem. A more robust alternative is to use something like Hangfire.

    QueueBackgroundWorkItem will register the work with ASP.NET. When ASP.NET has to recycle, it will notify the background work and will then wait up to 30 seconds for the work to complete. If the background work does not complete within that time, the work will disappear. 

    Simple Example:

            HostingEnvironment.QueueBackgroundWorkItem(ct =>
            {
                try
                {
                    // your code
                }
                catch (Exception ex)
                {
                    // handle & log exception
                }
            });
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 16, 2016 4:48 PM

All replies

  • User753101303 posted

    Hi,

    You mean a System.Component.BackgroundWorker? I'm even surprised that it works. It is intended to update a UI from a background process which doesn't make really sense for a web app (as the UI is rendered as HTML and then shown by the browser). To me it is rather intended for Windows based applications...

    Else it depends if at this point you are done with it. You still have some background work that runs or it is completed already at this step?

    Friday, December 16, 2016 4:31 PM
  • User765422875 posted

    Is the class you are using?

    https://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker(v=vs.110).aspx

    If it is, then no you do not need to dispose.

    BackgroundWorker derives from Component. Component implements the IDisposable interface. This makes BackgroundWorker inherit the Dispose() method. A Backgroundworker is a relatively 'light' object, it holds no resources. The thread is borrowed from the ThreadPool.

    If you are using ASP.NET and you want to run a background task, then at a minimum you should use HostingEnvironment.QueueBackgroundWorkItem. A more robust alternative is to use something like Hangfire.

    QueueBackgroundWorkItem will register the work with ASP.NET. When ASP.NET has to recycle, it will notify the background work and will then wait up to 30 seconds for the work to complete. If the background work does not complete within that time, the work will disappear. 

    Simple Example:

            HostingEnvironment.QueueBackgroundWorkItem(ct =>
            {
                try
                {
                    // your code
                }
                catch (Exception ex)
                {
                    // handle & log exception
                }
            });
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 16, 2016 4:48 PM
  • User314655009 posted

    Hello,

    Yes, I am using BackgroundWorker.

    What do you recommend?

    williamj

    Friday, December 16, 2016 7:40 PM
  • User765422875 posted

    See my answer, I've updated it.

    Friday, December 16, 2016 7:56 PM