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 6721213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:18:10+00:00 2026-05-26T09:18:10+00:00

In an application I’m developing, I have a main form that simply sits there

  • 0

In an application I’m developing, I have a main form that simply sits there and displays log data, and a worker thread that autonomously does the work in a loop.

MyWorker worker = new MyWorker();
MainForm mainForm = new MainForm();

// Subscribe form to log event so log data gets displayed
worker.Log += mainForm.Log;

// Start the worker thread's MainLoop
new Thread(new ThreadStart(worker.MainLoop)).Start();

// Show the form (blocking)
Application.Run(mainForm);

// If we end up here, the form has been closed and the worker has to stop running            
worker.Running = false;

As you can see, whenever the form is closed, the worker thread should be stopped. The worker looks like this:

public class MyWorker
{
    public String Running { get; set; }

    public MyWorker()
    {
        Running = true;
    }

    public void MainLoop()
    {

        while (Running)
        {

            DoExtensiveWork1();
            if (!Running) return;

            DoExtensiveWork2();
            if (!Running) return;

            DoExtensiveWork3();
            if (!Running) return;

            DoExtensiveWork4();
            if (!Running) return;

            DoExtensiveWork5();         
            if (!Running) return;

            // We have to wait fifteen minutes (900 seconds) 
            // before another "run" can be processed
            for (int i = 0; i < 900; i++)
            {
                Thread.Sleep(1000);
                if (!Running) return;
            }
        }
    }
}

As you can see, I want the thread to be able to stop when switching between successive work operations, but not when within an operation. When an operation (DoExtensiveWorkN) has finished, its status and results are persisted do disk or database, so quitting while an operation is in progress (by, for example, Thread.Abort) is not an option.

However, I find this code I’ve just written repulsive to look at, especially the “wait loop” which sleeps for one second 900 times, to prevent the thread from idling for 15 minutes before detecting Running has been set to false.

I’d rather be able to throw some kind of event to stop the main loop as soon as it’s finished a piece of work.

Can anyone point me in the right direction how to do this, or if a total rewrite is required because I totally misunderstood threading, show me somewhere where those principles are explained?

  • 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-05-26T09:18:11+00:00Added an answer on May 26, 2026 at 9:18 am

    You can tidy up both the running of the individual tasks and the 15 min wait loop considerably.

    I’d suggest perhaps using something like this:

    public class MyWorker
    {
        private readonly ManualResetEvent _stopEvent = new ManualResetEvent(false);
        private readonly Action[] _workUnits;
    
        private bool Running
        {
            get { return !_stopEvent.WaitOne(0); }
        }
    
        public MyWorker()
        {
            _workUnits = new Action[]
            {
                DoExtensiveWork1,
                DoExtensiveWork2,
                DoExtensiveWork3,
                DoExtensiveWork4,
                DoExtensiveWork5
            };
        }
    
        public void Stop()
        {
            _stopEvent.Set();
        }
    
        public void MainLoop()
        {
    
            while (Running)
            {
                foreach (var workUnit in _workUnits)
                {
                    workUnit();
                    if (!Running) return;
                }           
    
                // We have to wait fifteen minutes (900 seconds) 
                // before another "run" can be processed
                if (_stopEvent.WaitOne(900000)) return;
            }
        }
    }
    

    Then to stop the process at the next appropriate point:

    Worker.Stop();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Application use NHibernate. I Have object A that contains set of objects B. I
My application is suspending on a line of code that appears to have nothing
Application has an auxiliary thread. This thread is not meant to run all the
Application : HTA (therefore IE) This is an application that uses SendKeys to populate
Application stores configuration data in custom section of configuration file. This information is used
Application I am developing does some kind of server-side authorization. Communication is done via
The application I'm currently writing is using MVVM with the ViewModel-first pattern. I have
my application use 10 threads that to read a lot of html file.similar the
application = webapp.WSGIApplication( [(r'/main/profile/([a-f0-9]{40})', ProfileHandler)], debug=True) The regex in the above parameter will not
Application uses Entity Framework 4.1 with database first approach. I have in database a

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.