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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:58:22+00:00 2026-06-14T21:58:22+00:00

I have a Thread (STAThread) in a Windows Service, which performs a big amount

  • 0

I have a Thread (STAThread) in a Windows Service, which performs a big amount of work. When the windows service is restarted I want to stop this thread gracefully.

I know of a couple of ways

  • A volatile boolean
  • ManualResetEvent
  • CancellationToken

As far as I have found out Thread.Abort is a no go…

What is the best practice ?
The work is perfomed in another class than the one where the thread is started, so it is necessary to either introduce a cancellationToken parameter in a constructor or for example have a volatile variable. But I just can’t figure out what is smartest.

Update
Just to clarify a little I have wrapped up a very simple example of what I’m talking about. As said earlier, this is being done in a windows service. Right now I’m thinking a volatile boolean that is checked on in the loop or a cancellationToken….
I cannot wait for the loop to finish, as stated below it can take several minutes, making the system administrators of the server believe that something is wrong with the service when they need to restart it…. I can without problems just drop all the work within the loop without problems, however I cannot do this with a Thread.Abort it is “evil” and furthermore a COM interface is called, so a small clean up is needed.

Class Scheduler{
  private Thread apartmentThread;
  private Worker worker;

  void Scheduling(){
    worker = new Worker();
    apartmentThread = new Thread(Run);
    apartmentThread.SetApartmentState(ApartmentState.STA);
    apartmentThread.Start();    
  }

  private void Run() {
    while (!token.IsCancellationRequested) {
      Thread.Sleep(pollInterval * MillisecondsToSeconds);
      if (!token.IsCancellationRequested) {
        worker.DoWork();
      }
    }
  }
}

Class Worker{
  //This will take several minutes....
  public void DoWork(){
    for(int i = 0; i < 50000; i++){
      //Do some work including communication with a COM interface
      //Communication with COM interface doesn't take long
    }
  }
}

UPDATE
Just examined performance, using a cancellationToken where the isCancelled state is “examined” in the code, is much faster than using a waitOne on a ManualResetEventSlim. Some quick figuers, an if on the cancellationToken iterating 100.000.000 times in a for loop costs me approx. 500 ms, where the WaitOne costs approx. 3 seconds. So performance in this scenario it is faster to use the cancellationToken.

  • 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-14T21:58:24+00:00Added an answer on June 14, 2026 at 9:58 pm

    You haven’t posted enough of your implementation but I would highly recommend a CancellationToken if that is available to you. It’s simple enough to use and understand from a maintainability standpoint. You can setup cooperative cancellation as well too if you decide to have more than one worker thread.

    If you find yourself in a situation where this thread may block for long periods of time, it’s best to setup your architecture so that this doesn’t occur. You shouldn’t be starting threads that won’t play nice when you tell them to stop. If they don’t stop when you ask them, the only real way is to tear down the process and let the OS kill them.

    Eric Lippert posted a fantastic answer to a somewhat-related question here.

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

Sidebar

Related Questions

I have thread where downloading xml file a this xml file i want to
I have a thread which needs to be run in the background every 1000ms.
I have a thread from a thread pool in a service that does quite
I have several thread pools and I want my application to handle a cancel
I have a thread that does the following: 1) Do some work 2) Wait
I have a windows from application with a program.cs code like this: static class
I have this code. I want to exit my Java Application if it detects
I have this piece of code i want to synchronized between the StartWatch() and
I have a Windows Form, which is a modal mdi child, that is suppose
I have a C# Windows Service. The OnStart() method contains 3 lines that all

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.