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?
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.
Member
8 Points
106 Posts
Do I need to dispose a BackgroundWorker object that never completes?
Dec 16, 2016 03:05 PM|williamj7|LINK
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
All-Star
48280 Points
17983 Posts
Re: Do I need to dispose a BackgroundWorker object that never completes?
Dec 16, 2016 04:31 PM|PatriceSc|LINK
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?
Participant
1310 Points
442 Posts
Re: Do I need to dispose a BackgroundWorker object that never completes?
Dec 16, 2016 04:48 PM|deepalgorithm|LINK
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:
Member
8 Points
106 Posts
Re: Do I need to dispose a BackgroundWorker object that never completes?
Dec 16, 2016 07:40 PM|williamj7|LINK
Hello,
Yes, I am using BackgroundWorker.
What do you recommend?
williamj
Participant
1310 Points
442 Posts
Re: Do I need to dispose a BackgroundWorker object that never completes?
Dec 16, 2016 07:56 PM|deepalgorithm|LINK
See my answer, I've updated it.