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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:35:57+00:00 2026-05-26T11:35:57+00:00

Sorry if this is a duplicate, but I’m not quite sure what terms I

  • 0

Sorry if this is a duplicate, but I’m not quite sure what terms I need to use to find existing answers to this question.

I’m trying to improve start-up performance of an application, the pseudo-code looks a bit like this.

LoadBigFileFromDisk();  //slow
SetupNetwork();         //even slower
UseBigFileFromDisk();

I figured that as the first step is disk-bound, and the other network-bound (and slower), I could run the first in a background thread (currently playing with ThreadPool.QueueUserWorkItem, but not sure if that’s the best way) and improve the performance a bit.

It works, but what worries me is that I’m relying on the second step being slow enough for the first to complete.

I know I could set a _done boolean somewhere and while ! on that, but is there a more elegant/idiomatic solution?

(Not .Net 4.0 yet, so though I’m interested in Task-based, I need the fall-back solutions).

  • 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-26T11:35:57+00:00Added an answer on May 26, 2026 at 11:35 am

    In the “main class” do this:

    ManualResetEvent mre = new ManualResetEvent(false);
    

    in your “main” method do this:

    // Launch the tasks
    
    mre.WaitOne();
    

    in the task when it finishes (just before the return 🙂 )

    mre.Set();
    

    If you have to wait for multiple events, in your “main” create multiple ManualResetEvent and put them in an array, each event “connected” to one of the tasks, and then each task Sets its event when it finishes. Then in your “main” you do:

    WaitHandle.WaitAll(arrayOfManualResetEvents);
    

    Note that in this way you can wait up to 64 events. If you need more, there is another method (and note that you have to use this last method even if you are on a STA thread, like the main thread of a WinForm app).

    ManualResetEvent mre = new ManualResetEvent(false);
    int remaining = xxx; // Number of "tasks" that must be executed.
    
    // Launch tasks
    
    mre.WaitOne();
    

    At the end of each task

    if (Interlocked.Decrement(ref remaining) == 0)
    {
        mre.Set();
    }
    

    Only the last task will decrement the remaining field to 0 and mre.Set().

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

Sidebar

Related Questions

I'm sorry if this is a duplicate, but the question search terms are pretty
Sorry if this is a duplicate but I couldn't find any satisfying answers in
I am sorry if this is a duplicate but I was not able to
Sorry if this is duplicate,I would think it would be but couldn't find anything.
Sorry if this is a duplicate question...I've searched around and found similar advice but
I am sorry if this is a duplicate, but I cannot find the answer
I am sorry that I duplicate this question , but I don't have the
Sorry if this is a duplicate question but google isn't smart enough to understand
Sorry if this is a duplicate question. There are quite a few questions around
Sorry, I'm sure there's a duplicate of this but I'm really new to jQuery.

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.