Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You have some options. If you don't care if the operation completes when the application is going down you might be better off using a ThreadPool thread via <a href="http://msdn.microsoft.com/en-us/library/system.threading.threadpool.queueuserworkitem.aspx" rel="nofollow">QueueUserWorkItem</a> or (as Servy suggests in comments) set the <a href="http://msdn.microsoft.com/en-us/library/system.threading.thread.isbackground.aspx" rel="nofollow">IsBackground</a> property of your thread to true, which will allow the process to exit without the thread exiting.</p> <p>If you do care about the operation completing and/or have cleanup logic that needs to be run on shutdown you probably don't really want to use Thread.Abort, at least not as your goto strategy. What I use is something similar to this:</p> <pre><code>public abstract class DisposableThread : IDisposable { private ManualResetEvent exiting = new ManualResetEvent(false); private Thread theThread; private TimeSpan abortTimeout; public DisposableThread(): this(TimeSpan.FromMilliseconds(100)) { } public DisposableThread(TimeSpan abortTimeout) { this.abortTimeout = abortTimeout; theThread = new Thread((_) =&gt; ThreadProc()); } protected virtual void ThreadProc() { while(!exiting.WaitOne(0)) { WorkUnit(exiting); } ThreadCleanup(); } public void Dispose() { this.Dispose(true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { exiting.Set(); if (!theThread.Join(abortTimeout)) { // logme -- the thread didn't shutdown gracefully theThread.Abort(); while (!theThread.Join(1000)) { // logme -- the thread is doing something dumb in an exception handler } } exiting.Dispose(); } // WorkUnit should return as quickly as safe if the exiting handle is set // If it doesn't the thread will be aborted if it takes longer than abortTimeout protected abstract void WorkUnit(WaitHandle exiting); // override if you need to cleanup on exit protected virtual void ThreadCleanup() { } } </code></pre> <p>Which gives your thread a chance to exit gracefully and only aborts if a graceful exit fails.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload