I just want to know how we can kill the BackgroundWorker Thread once it finished up with its associated tasks. Is there any method like Kill or Abort or something else which can help?
-
Language/Framework? I know of a `BackgroundWorker` class in .NET, but have no way to know if that's the one you're talking about. Please edit your question and add suitable language/framework/library tags – Damien_The_Unbeliever Jun 18 '13 at 12:40
-
1I am talking about the class BackgroundWorker..which we can use to make a TimeTaking task go async.. – Vishal Jun 18 '13 at 12:46
-
If it's .Net, then CancelAsync(), but that's used for when it's doing work. You can't kill it or abort it post op. – Big Daddy Jun 18 '13 at 13:06
-
CAncelAsync() just cancels the task the thread is performing..it doesnt kill the thread.. – Vishal Jun 18 '13 at 13:09
-
Right, check this http://stackoverflow.com/questions/800767/how-to-kill-background-worker-completely – Big Daddy Jun 18 '13 at 13:14
-
1So you *are* talking about .NET? Stack Overflow is a site that caters for programmers of all languages, all frameworks, all libraries. You need to add suitable tags to questions so that the right people look at it. And so that people familiar with a class of the same name in some completely *different* technology don't end up wasting their time. – Damien_The_Unbeliever Jun 18 '13 at 13:33
-
Calling Thread.Abort() requires a reference to a Thread object. That's hard to come by for a BGW since it doesn't use a Thread, it uses a threadpool thread. Better that way, write your code to pay attention to the CancellationPending property. – Hans Passant Jun 18 '13 at 17:18
2 Answers
Why are you worried about closing the thread after the task is already completed?
The thread is cleaned up in garbage collection. Calling Dispose() will help it probably be cleaned up sooner and is 'best practice'.
BackgroundWorker tasks are meant to be used on WinForms or using ThreadPool.QueueUserWorkItem(...) which will handle the lifecycle of the thread automatically internally.
See this related question: How to "kill" background worker completely?
No, you can't in general kill the thread that the BackgroundWorker
was running on. That thread is part of ThreadPool. The ThreadPool
manages the threads and decides when it should allocate more or get rid of some. You don't need to worry about killing the thread.
Which shouldn't be a problem. It's not like the thread is using any CPU time while it's idle. The small amount of system resources it's occupying won't be noticed.

- 131,090
- 20
- 188
- 351