title: How to stop looper execution?
body: <p>I have a looper thread that is running always and pull tasks from queue in order to execute them.</p>
  
  <p>There is a main looper method</p>
  
  <pre><code>public void Start()
          {
              m_looperThread = new Thread(() =&gt;
               {
                   while (true)
                   {
                      //Dequeue use lock inside
                      TASK_TYPE task = m_taskList.GetAndRemoveFirst();
  
                       if (task == null || !m_isThreadRunning)
                       {
                           break;
                       }
  
                       task.Execute();
                       FinishedTask(task);
                   }
               }
                  )
              {
                  IsBackground = true
              };
  
              m_looperThread.Start();
          }
  </code></pre>
  
  <p>Now if I need to close an application, user need to click red cross and before closing I need to abort all looper tasks. </p>
  
  <p>How I do it</p>
  
  <pre><code>        public void Abort()
          {
              Put(default(TASK_TYPE));
              m_isThreadRunning = false;
  
              if (m_looperThread != null)
              {
                  m_looperThread.Join();
              }
  
              m_taskList.Clear();
          }
  </code></pre>
  
  <p>So, first of all I put a null item and additional I set <code>m_isThreadRunning</code> value to false because in looper method I am checking this values in order to stop it. Then I call <code>.Join()</code> to make sure that I finished this thread and clear task list. Now I know exactly that all tasks were aborted, threads were joined and all is fine. </p>
  
  <p>But issue here in this line</p>
  
  <pre><code>task.Execute();
  </code></pre>
  
  <p>What is the issue - when task run and user need to close the application I can't <code>.Join</code> the looper thread before task finish the job (it could be 1 minute). And finally it is looks like application in stuck.</p>
  
  <p>So, question is - how correctly <code>.join</code> the thread and give to user opportunity to close the application?</p>