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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:13:56+00:00 2026-06-12T23:13:56+00:00

I have a Task that reads strings from a blocking collection and is supposed

  • 0

I have a Task that reads strings from a blocking collection and is supposed to write them out to a file. Trouble is, while the file is created, the size of the file is 0 bytes after the task completes.

While debugging, I see that non-empty lines are retrieved from the blocking collection, and the stream writer is wrapped in a using block.

For debugging I threw in a flush that should not be required and write the lines to the console. There are 100 non-empty lines of text read from the blocking collection.

// Stuff is placed in writeQueue from a different task  
BlockingCollection<string> writeQueue = new BlockingCollection<string>();

Task writer = Task.Factory.StartNew(() => 
{
    try
    {
        while (true)
        {
            using (FileStream fsOut = new FileStream(destinationPath, FileMode.Create, FileAccess.Write))
            using (BufferedStream bsOut = new BufferedStream(fsOut))
            using (StreamWriter sw = new StreamWriter(bsOut))
            {
                string line = writeQueue.Take();
                Console.WriteLine(line); // Stuff is written to the console
                sw.WriteLine(line);
                sw.Flush(); // Just in case, makes no difference
            }
        }
    }
    catch (InvalidOperationException)
    {
        // We're done.
    }
});

Stepping through in the debugger, I see that the program terminates in an orderly manner. There are no unhandled exceptions.

What might be going wrong here?

  • 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-12T23:13:57+00:00Added an answer on June 12, 2026 at 11:13 pm

    You are re-creating the file on every run of the loop. Change the FileMode.Create to FileMode.Append and it will keep the previous values you wrote on it.

    Also, using exceptions to detect that you should stop is a really bad practice, if this a consumer-producer solution, you can easily do better by having the producer setting a thread safe flag variable signaling it has finished the work and will not produce anything else.

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

Sidebar

Related Questions

I have Async Task that must write folder Files from assets to folder on
In my rails application I have a rake task that reads emails using imap.
I have a main activity that takes elements from a database and displays them
Overview of My Situation: My task is to read strings from a file, and
I have been given a simple task, that I cannot seem to figure out
I have a complex database conversion console app that reads from an old database,
I have a thread running in a service of an app that reads out
I've been developing an app that reads a .csv file, takes out approximately 16
I have a task that runs periodically 10 second. I do some picturebox refreshing
I have a task that takes a rather long time and should run in

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.