Closing form with running tasks

I have a WinForms application that consists of a main UI thread and 4 tasks. My main form has a member level variable like this:

private bool keepThreadsRunning = false;

In the Load () event of my main form, I have the following:

keepThreadsRunning = true;
var task1Worker = Task.Factory.StartNew(() => DoStuff1());
var task2Worker = Task.Factory.StartNew(() => DoStuff2());
var task3Worker = Task.Factory.StartNew(() => DoStuff3());
var task4Worker = Task.Factory.StartNew(() => DoStuff4());

Inside each of my DoStuff () methods, I basically have this:

while (keepThreadsRunning)
{
  // do work here
  Thread.Sleep(30000); // a couple of my tasks only need to run every 30 seconds or so
}

Finally, in my Form_Closing () event handler, I have the following:

keepThreadsRunning = false;
this.Close();

Watching my application in the task manager, it seems that the process ends when I close the form, but I got a little confused in the four tasks. Is my call to this.Close () really causing these tasks to complete (even if they are in the Thread.Sleep () call when this happens)? And is there a better way to do this than the way I code it now?

EDIT. ( ), , , , . , 30 , , 30s-wait ( Thread.Sleep()), .

+5
3

, . , , , Windows.

, , , , , , Windows - Windows. , , .

, , :

using System;
using System.Timers;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            var timer1 = new System.Timers.Timer { Interval = 30000, Enabled = true };
            var timer2 = new System.Timers.Timer { Interval = 20000, Enabled = true };
            var timer3 = new System.Timers.Timer { Interval = 10000, Enabled = true };
            var timer4 = new System.Timers.Timer { Interval = 5000, Enabled = true };

            timer1.Elapsed += timer1_Elapsed;
            timer2.Elapsed += timer2_Elapsed;
            timer3.Elapsed += timer3_Elapsed;
            timer4.Elapsed += timer4_Elapsed;
        }

        void timer4_Elapsed(object sender, ElapsedEventArgs e)
        {
            //do work here
        }

        void timer3_Elapsed(object sender, ElapsedEventArgs e)
        {
            //do work here
        }

        void timer2_Elapsed(object sender, ElapsedEventArgs e)
        {
            //do work here
        }

        void timer1_Elapsed(object sender, ElapsedEventArgs e)
        {
            //do work here
        }
    }
}
+2

Thread.Sleep() WaitHandle, ManualResetEvent, :

var threadTerminationHandle = new ManualResetEvent(false);

:

do {
  // do work here
} while (!threadTerminationHandle.WaitOne(TimeSpan.FromSeconds(30))

WaitHandle 30 , , .

:

threadTerminationHandle.Set();
Close();
+3

When you close the application, the tasks will be closed accordingly, because the task is processed in the background thread from the thread pool. Thus, you do not need to periodically check the cancellation token to determine whether they have been canceled.

+2
source

All Articles