0

There are two threads: main and additional. Addidional thread works in infinite loop and should be stopped after the main thread ends or application ends. Could you tell me the best way to tell AdditionalThread that it should be stopped? Maybe i should use some event, which is automatically generated after "return" in main thread or maybe check somehow if main thread .IsAlive() or i should use AppDomain.ProcessExit?

void main()
{
   Thread.CurrentThread.Name = "MainThread";

   Thread addThread = new Thread(DoSomething)
   addThread.Name = "AdditionalThread";
   addThread.IsBackground = false;
   addThread.Start();

   return 0;
}
DoSomething()
{
   While( true || /* while mainthread is working */)
   { 
      /* do something */
   }

}

Thank you!

hyWhy
  • 23
  • 5
  • Check out [TPL and Cancellation](http://msdn.microsoft.com/en-us/library/dd997364%28v=vs.110%29.aspx) – Sriram Sakthivel Jul 14 '14 at 08:16
  • Then why are you explicitly making it a non-background thread? The whole point of such threads is that they're stopped when all non-background threads stop, and the application is exiting. It seems like you're deliberately turning away from the feature you should use. – Damien_The_Unbeliever Jul 14 '14 at 09:03
  • Because i have a bigger problem and trying to solve it ( http://stackoverflow.com/q/24698785/3829212 ) - this nonbackground thread is a logging queue, which can be nonempty at the moment when mainthread ends, and then this queue should be processed and only after that the whole application could be closed – hyWhy Jul 14 '14 at 09:12
  • Your question boils down to: How can I signal my background thread to stop itself? The duplicate answers that. – usr Jul 14 '14 at 10:34

0 Answers0