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

  • Home
  • SEARCH
  • 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 7917711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T15:14:36+00:00 2026-06-03T15:14:36+00:00

I have a windows service which will start and stop the execution of some

  • 0

I have a windows service which will start and stop the execution of some process that is being done with the held of Threads.

I have two classes as follows:

public class PerformTask
{
    Thread _thread = null;
    public void StartTask()
    {
        _thread = new Thread(new ThreadStart(DoSomeWork));
        _thread.Start();
    }

    public void DoSomeWork()
    {
        // Do Some Work
        _thread = null;
    }

    public void Abort()
    {
        if (_thread != null)
        {
            try
            {
                _thread.Abort();
            }
            catch (ThreadAbortException) {}
        }
    }
}

public class Engine
{
    List<PerformTask> _task = new List<PerformTask>();
    public void Start()
    {
        var task = new PerformTask();
        _task.Add(task);
        // Add task to the timed action queue
        _actionQueue.Add(s => task.StartTask(), TimeSpan.FromSeconds(10));
    }

    public void Stop()
    {
        _task.ForEach(task => task.Abort());
        _task.Clear();
        _actionQueue.Stop();
        _actionQueue.Clear();
    }
}

The _actionQueue is a custom defined source code developed to perform a specified action at a recurring time interval specified. All the actions are kept in queue and invoked at the specified time interval.

Now, the Windows service’s OnStart and OnStop method would call Engine class’ Start and Stop method respectively.

What I want is when the windows service is stopped, all the threads that are running should stop their processing/execution.

But, what is happening here is as new thread instance is being created in I have a windows service which will start and stop the execution of some process that is being done with the held of Threads.

I have two classes as follows:

public class PerformTask
{
    Thread _thread = null;
    public void StartTask()
    {
        _thread = new Thread(new ThreadStart(DoSomeWork));
        _thread.Start();
    }

    public void DoSomeWork()
    {
        // Do Some Work
        _thread = null;
    }

    public void Abort()
    {
        if (_thread != null)
        {
            try
            {
                _thread.Abort();
            }
            catch (ThreadAbortException) {}
        }
    }
}

public class Engine
{
    List<PerformTask> _task = new List<PerformTask>();
    ActionQueue _actionQueue = new ActionQueue();
    public void Start()
    {
        foreach(.....)
        {
            var task = new PerformTask();
            _task.Add(task);
            // Add task to the timed action queue
            _actionQueue.Add(s => task.StartTask(), TimeSpan.FromSeconds(10));
        }
        _actionQueue.Start();
    }

    public void Stop()
    {
        _task.ForEach(task => task.Abort());
        _task.Clear();
        _actionQueue.Stop();
        _actionQueue.Clear();
    }
}

The ActionQueue is a custom defined source code developed to perform a specified action at a recurring time interval specified. All the actions are kept in queue and invoked at the specified time interval.

Now, the Windows service’s OnStart and OnStop method would call Engine class’ Start and Stop method respectively.

What I want is when the windows service is stopped, all the threads that are running should stop their processing/execution.

But, what is happening here is as new thread instance is being created in StartTask method, when I call the
_task.ForEach(task => task.Abort())
I do not have the correct instance of Thread, that is all the instance of
_thread = new Thread(….);
is not being accessed, as there would a multiple queues for the same PerformTask class.

Note: I cannot make changes to the ActionQueue.

  1. Is Abort method a correct way of stopping the threads?

  2. How can I stop all the threads (including all the instances of Thread class created by the source code)?

  • 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-03T15:14:37+00:00Added an answer on June 3, 2026 at 3:14 pm

    Usually you’d create a WaitHandle (a ManualResetEvent for example) like that:

    ManualResetEvent stopAllThreads = new ManualResetEvent(false);
    

    So the event is “not set”. Change the loops in your thread method so that they loop until all work is done or the manual reset event is set.

    while (!stopAllThreads.WaitOne(50))
    

    or similar.

    Then, in the service’s OnStop method, you simply set the event (don’t forget to reset it again in OnStart, otherwise the threads will not run again when the service is restarted):

    stopAllThreads.Set();
    

    and wait for all the threads to finish.

    Actually, aborting threads is not a good way to stop threads – you should always go for something like the above.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple WIX installer which will install a Windows Service. I have
I have a basic windows service which does some conversions of data. There's decoupled
i have to develop windows service which will copy files to different servers. So
I have an windows service that monitors a folder for new files being created.when
Is there a command that will stop the execution of my program? I have
We have built a Windows Service that is running on client's machines, which occasionally
I have a program that runs as a Windows Service which is processing files
I have created a C# .Net windows service. There are some configuration files which
I'm writing a windows service which will be used to monitor some other services
I have a Windows Service which is basically accessing a mailbox and reading the

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.