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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:15:44+00:00 2026-05-14T03:15:44+00:00

Greetings I have a program that creates multiples instances of a class, runs the

  • 0

Greetings

I have a program that creates multiples instances of a class, runs the same long-running Update method on all instances and waits for completion. I’m following Kev’s approach from this question of adding the Update to ThreadPool.QueueUserWorkItem.

In the main prog., I’m sleeping for a few minutes and checking a Boolean in the last child to see if done

while(!child[child.Length-1].isFinished) {
    Thread.Sleep(...);
}

This solution is working the way I want, but is there a better way to do this? Both for the independent instances and checking if all work is done.

Thanks

UPDATE:
There doesn’t need to be locking. The different instances each have a different web service url they request from, and do similar work on the response. They’re all doing their own thing.

  • 1 1 Answer
  • 1 View
  • 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-14T03:15:44+00:00Added an answer on May 14, 2026 at 3:15 am

    If you know the number of operations that will be performed, use a countdown and an event:

    Activity[] activities = GetActivities();
    int remaining = activities.Length;
    using (ManualResetEvent finishedEvent = new ManualResetEvent(false))
    {
        foreach (Activity activity in activities)
        {
            ThreadPool.QueueUserWorkItem(s =>
            {
                activity.Run();
                if (Interlocked.Decrement(ref remaining) == 0)
                    finishedEvent.Set();
            });
        }
        finishedEvent.WaitOne();
    }
    

    Don’t poll for completion. The .NET Framework (and the Windows OS in general) has a number of threading primitives specifically designed to prevent the need for spinlocks, and a polling loop with Sleep is really just a slow spinlock.

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

Sidebar

Related Questions

Greetings all, I’m working on a C# program that requires being able to get
Greetings all, I have a code snippet as follows : class AppCtx { private:
greetings all I have a cronExpression that I want it to be started on
Greetings! I have a Repeater control that's using an XmlDataSource control. <asp:FormView id=myFormView runat=server
Greetings, I have some mysql tables that are currently using an md5 hash as
Greetings all, I have a question. On my MOSS 2007 dev box, I created
Greetings, I have to following problem. I have a WCF Service which runs under
Greetings, stack overflow In my database, I already have one table, 'contacts' that contains
Season's greetings! I have a function that prints out the contents of a char**
I have a program below that tries to take input from the user and

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.