0

I have a C# application using the .NET Compact Framework 3.5 that opens multiple forms depending on the user interaction. In one particular form, I have a background thread that periodically checks the battery life of the Windows CE device the application runs on. Note, this is not the form called in Main(). For example, Application.Run(new MyOtherForm()); is called in Main().

public MyForm()
{
    Thread mythread = new Thread(checkBatteryLife);
    mythread.IsBackground = true;
    mythread.Start();
}

private void checkBatteryLife()
{
    while(true)
    {
        // Get battery life

        Thread.Sleep(1000);
    }
}

My question is, when MyForm closes, does the background thread stop as well? Or will it stop when the application exists (when Main() finishes processing)? If the background threads ends when the application closes, I found this workaround, but seems unnecessary if the thread stops at the time the form closes.

EDIT: I opted to use a System.Threading.Timer instead of a Thread.

private System.Threading.Timer batteryLifeTimer;

public MyForm()
{
    AutoResetEvent autoEvent = new AutoResetEvent(false);
    TimerCallback tcb = checkBatteryLife;
    this.batteryLifeTimer = new System.Threading.Timer(tcb, autoEvent, 1000, 10000);
}

private void checkBatteryLife(Object stateInfo)
{
    // Get battery life.
    // Update UI if battery life percent changed.
}

private void MyForm_Closing(object sender, CancelEventArgs e)
{
    this.batteryLifeTimer.Dispose();
}
JT9
  • 914
  • 5
  • 13
  • 22
  • 1
    Try look at here: http://stackoverflow.com/questions/23829532/when-does-background-threads-prevent-the-process-of-being-terminated – Stefano Bafaro Oct 28 '15 at 15:15
  • 2
    Why did you choose using a thread over a timer for something like this? – Ron Beyer Oct 28 '15 at 15:18
  • I guess it would depend how intense this checking the battery is - if that's a blocking task then I can see reasons to handle it in a background thread. I would have expected the thread to run until the form is disposed - and the thread will certainly stop when the process does - there being no process to host it... – Martin Milan Oct 28 '15 at 15:21
  • 1
    @MartinMilan How long it takes to run in no way changes that this belongs in a `Timer`. It would, at most, determine which type of `Timer` you use. – Servy Oct 28 '15 at 15:26
  • Sorry Servy (who I'll defer to happily...) - My thinking was to keep the operation from blocking the UI thread - should have thought to use a System.Timers.Timer... – Martin Milan Oct 28 '15 at 15:36
  • I reworked my form to use a `Timer`instead because checking the battery life is so quick. I've posted an edit above. – JT9 Oct 28 '15 at 16:42

1 Answers1

4

The background thread will stop executing when it either finishes executing the delegate, or when there are no more foreground threads in the entire process.

Servy
  • 202,030
  • 26
  • 332
  • 449
  • Is the "entire process" from the point of view of Main()? Or from the point of view of the form? – JT9 Oct 28 '15 at 15:22
  • 1
    Neither represents the entire process. `Main` is just one method being called by one thread. The form is one object created in one method in one thread. The process is the collection of all of the threads that are running. – Servy Oct 28 '15 at 15:25