<p>is there a way to abort threads created with QueueUserWorkItem?</p>

<p>Or maybe I don't need to? What happens if the main application exits? Are all thread created from it aborted automatically?</p>

[{'answer_id': 26820, 'author': 'Vaibhav', 'author_id': 380, 'author_profile': 'https://Stackoverflow.com/users/380', 'pm_score': 2, 'selected': False, 'text': '<p>Yes, they will. However, if you are using unmanaged resources in those threads, you may end up in a lot of trouble.</p>\n'}, {'answer_id': 26830, 'author': 'Judah Gabriel Himango', 'author_id': 536, 'author_profile': 'https://Stackoverflow.com/users/536', 'pm_score': 5, 'selected': True, 'text': "<p>You don't need to abort them. When your application exits, .NET will kill any threads with IsBackground = true. The .NET threadpool has all its threads set to IsBackground = true, so you don't have to worry about it.</p>\n\n<p>Now if you're creating threads by newing up the Thread class, then you'll either need to abort them or set their IsBackground property to true.</p>\n"}, {'answer_id': 26839, 'author': 'OJ.', 'author_id': 611, 'author_profile': 'https://Stackoverflow.com/users/611', 'pm_score': 2, 'selected': False, 'text': "<p>The threadpool uses background threads. Hence, they will all be closed automatically when the application exits.</p>\n\n<p>If you want to abort a thread yourself, you'll have to either manage the thread yourself (so you can call Thread.Abort() on the thread object) or you will have to set up some form of notification mechanism which will let you tell the thread that it should abort itself.</p>\n"}, {'answer_id': 32959, 'author': 'Will Dean', 'author_id': 987, 'author_profile': 'https://Stackoverflow.com/users/987', 'pm_score': 3, 'selected': False, 'text': "<blockquote>\n  <p>However, if you are using unmanaged\n  resources in those threads, you may\n  end up in a lot of trouble.</p>\n</blockquote>\n\n<p>That would rather depend how you were using them - if these unmanaged resources were properly wrapped then they'd be dealt with by their wrapper finalization regardless of the mechanism used to kill threads which had referenced them. <em>And unmanaged resources are freed up by the OS when an app exits anyway.</em></p>\n\n<p>There is a general feeling that (Windows) applications spend much too much time trying to clean-up on app shutdown - often involving paging-in huge amounts of memory just so that it can be discarded again (or paging-in code which runs around freeing unmangaged objects which the OS would deal with anyway).</p>\n"}, {'answer_id': 896492, 'author': 'Lukas Šalkauskas', 'author_id': 5369, 'author_profile': 'https://Stackoverflow.com/users/5369', 'pm_score': 0, 'selected': False, 'text': '<p>yeah, they are background, but f.ex if you have application where you use ThreadPool for some kinda multiple downloading or stuff, and you want to stop them, how do you stop ? my suggestion would be: \nexit thread asap, f.ex </p>\n\n<pre><code>bool stop = false;\nvoid doDownloadWork(object s) \n{\n   if (!stop)\n   {\n       DownloadLink((String)s, location);\n   }\n}\n</code></pre>\n\n<p>and if you set stop = true, second (currently in queue) threads automatically exit, after queue threads finishes it process.</p>\n'}, {'answer_id': 975941, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': "<p>According to Lukas Šalkauskas' answer.</p>\n\n<p>But you should use:</p>\n\n<pre><code>volatile bool stop = false;\n</code></pre>\n\n<p>to tell the compiler this variable is used by several threads.</p>\n"}]
['https://Stackoverflow.com/questions/26816', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1782/']