Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8615065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:16:50+00:00 2026-06-12T05:16:50+00:00

I have a WinForms application that consists of a main UI thread and 4

  • 0

I have a WinForms application that consists of a main UI thread and 4 tasks. My main form has a private 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 of 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
}

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

keepThreadsRunning = false;
this.Close();

Watching my application in task manager, it appears that the process is ending when I close my form but I’m a little confused about the four tasks. Is my call to this.Close() really causing those tasks to terminate (even if they’re in the Thread.Sleep() call when it happens)? And is there a better way of accomplishing this than the way I’m coding it right now?

EDIT – I’ve looked briefly at task cancellation (when my app exits) but my understanding is that my tasks would need to periodically check the cancellation token to determine if they’ve been cancelled. Given that some of my tasks need to run every 30 seconds, I couldn’t figure out how I’d implement that 30s wait (currently a Thread.Sleep()) and still have the task be checking the cancellation token.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T05:16:52+00:00Added an answer on June 12, 2026 at 5:16 am

    First of all, closing the main UI thread will terminate your other tasks. If you need them to keep running, maybe consider running them in a seperate Console Application, or a Windows Service.

    Even if you found a way to delay the closing of the form while you finish running the methods you need to run, this would only work if the end user closed the form in the way you wanted, and Windows being Windows there are a million and one ways to close an application so there is no guarantee that this will work.

    For running a method asynchronously every x amount of seconds, you could just use a timer for the whole thing, like so:

    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
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a winforms application that doesn't really have a main form - it's
I have a Winforms application that dynamically instantiates external form objects for use in
I have a winforms application that includes an Administration form (call it 'adminForm') and
I have a winforms application that has one tabcontrol with 2 pages. On both
I have a winforms application that I'd like to add a WPF user control
I have a c# .net 2.0 winforms application which consists basically of one form
I have a Winforms application that uses show multiple top-level windows: Form1 form1 =
I have a C# WinForms application that has a WebBrowser control inside of it.
I have a MDI WinForms application that can perform several tasks. Each task is
I have a WinForms application that I'd like to have ability to run as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.