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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:50:53+00:00 2026-05-16T07:50:53+00:00

I am writing a windows service that uses ThreadPool.QueueUserWorkItem() . Each thread is a

  • 0

I am writing a windows service that uses ThreadPool.QueueUserWorkItem(). Each thread is a short-lived task.

When the service is stopped, I need to make sure that all the threads that are currently executing complete. Is there some way of waiting until the queue clears itself?

  • 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-16T07:50:53+00:00Added an answer on May 16, 2026 at 7:50 am

    You could create an event (e.g. ManualResetEvent) in each thread, and keep it in a synchronised list (using the lock construct). Set the event or remove it from the list when the task is finished.

    When you want to join, you can use WaitHandle.WaitAll (MSDN documentation) to wait for all the events to be signalled.

    It’s a hack, but I can’t see how to reduce it to anything simpler!


    Edit: additionally, you could ensure that no new events get posted, then wait a couple of seconds. If they are indeed short-lived, you’ll have no problem. Even simpler, but more hacky.

    Finally, if it’s just a short amount of time, the service won’t exit until all threads have died (unless they are background threads); so if it’s a short amount of time, the service control manager won’t mind a second or so – you can just leave them to expire – in my experience.

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

Sidebar

Ask A Question

Stats

  • Questions 516k
  • Answers 516k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Hated. I found it. Turns out the reference was being… May 16, 2026 at 6:50 pm
  • Editorial Team
    Editorial Team added an answer This will return a list of unique IP address you're… May 16, 2026 at 6:50 pm
  • Editorial Team
    Editorial Team added an answer OK, I got the code working. I got it to… May 16, 2026 at 6:50 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I am writing WCF service that uses wsHttpBinding binding, which is not hosted in
Background I have a Windows service that uses various third-party DLLs to perform work
I am writing a Windows service that pulls messages from an MSMQ and posts
I'm writing an ASP.net application that uses Windows Identity Foundation. My ASP.net application uses
I'm writing a Windows Service using C# .NET 2005. How can I determine who
I'm writing a Windows Service application which listens for connections and performs certain tasks
I'm writing some software that modifies a Windows Server's configuration (things like MS-DNS, IIS,
I'm writing a windows service to do some daily processing, and I want to
I am using a custom EventLog for my Windows service. The service creates the
I have a windows service writes its log in a text file in 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.