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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:01:04+00:00 2026-06-14T08:01:04+00:00

I’m writing a C# console app and having an issue where the primary thread

  • 0

I’m writing a C# console app and having an issue where the primary thread closes before all the spawned off Tasks have a chance to complete. General program flow looks like this:

The program takes the path to a directory as a command line parameter. It then iterates over every file in that directory matching a specific extension and spawns a new task for each matching file to open up and analyze that file:

Directory.GetFiles(directoryPath).ToList<string>().Where(x => Path.GetExtension(x) == ".xfast").ToList().ForEach(x => ProcessXFastFile(x));

Each of these files contains a large number of CSV values corresponding back to a unique product key. For example, one file might contain 1 million CSV lines and 400 unique product keys. The point of the program is to sort all of the lines based on the product key and write them back out to individual files based on the key.

So in the ProcessXFastFile method I create a Dictionary<string, List<string>> using the product codes as Key values and adding each CSV line that matches that key into the corresponding list.

Once the sorting is finished, I iterate over the dictionary and spawn off a new task for each Key value and write out the CSV lines in that List:

foreach (KeyValuePair<string, List<string>> entry in productDictionary) {
    string writePath = String.Format(@"{0}\{1}-{2}.txt", directoryPath, hour, entry.Key);
    List<string> quotes = entry.Value;
    Task writeFileTask = Task.Factory.StartNew(() => WriteProductFile(writePath, quotes));
}

The problem is that the primary thread is ending before each of these Tasks to WriteProductFile have a chance to finish writing out their data. For example, I use one file as a control test and know that at there are ~450 unique keys in the dictionary so there should be that many corresponding files written out. However, only about 10 files get a chance to write out before the program ends.

How can I ensure that my program stays live long enough for all Tasks to finish? Any help is appreciated.

  • 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-14T08:01:06+00:00Added an answer on June 14, 2026 at 8:01 am

    You can put all of the tasks you get into a List<Task> instead of just creating them and tossing them, and then use the Task.WaitAll() to wait for completion:

    var tasks = new List<Task>();
    
    foreach (KeyValuePair<string, List<string>> entry in productDictionary) {
        string writePath = String.Format(@"{0}\{1}-{2}.txt", directoryPath, hour, entry.Key);
        List<string> quotes = entry.Value;
    
        // add task to list
        tasks.Add(Task.Factory.StartNew(() => WriteProductFile(writePath, quotes)));
    }
    
    // now wait for all tasks to finish, you'd also want to handle  exceptions of course.
    Task.WaitAll(tasks.ToArray());
    

    There are many variations of WaitAll(), you could wait indefinitely (as above), or wait for a TimeSpan and if time-out then print a progress message and wait again, etc…

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
We're building an app, our first using Rails 3, and we're having to build
I have a text area in my form which accepts all possible characters from
I am writing an app with both english and french support. The app requests
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I have a small JavaScript validation script that validates inputs based on Regex. I

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.