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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:36:50+00:00 2026-05-27T12:36:50+00:00

What is the best option for writing (appending) records to file in highly parallel

  • 0

What is the best option for writing (appending) records to file in highly parallel web environment in .net4 IIS7? I use ashx http handler to receive small portions of data that should be written to file quickly. First I used:

    using (var stream = new FileStream(fileName, FileMode.Append, FileAccess.Write, FileShare.ReadWrite, 8192))
    {
        stream.Write(buffer, 0, buffer.Length);
    } 

But I noticed that some records were broken or incomplete, probably because of FileShare.ReadWrite. Next I tried to chage it to FileShare.Read. There where no broken records then, but from time to time I got this exception: System.IO.IOException: The process cannot access the file … because it is being used by another process.

Ideally I would like the operating system to queue concurrent write requests so that all the records would be written eventually. What file access API should I use?

  • 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-27T12:36:50+00:00Added an answer on May 27, 2026 at 12:36 pm

    there are two options, depending on the size. If the size is small, probably the best option is to simply synchronize access to the file by some shared lock. If possible, it would also be a good idea to keep the file open (flushing occasionally), rather than constantly open/close. For example:

    class MeaningfulName : IDisposable {
        FileStream file;
        readonly object syncLock = new object();
        public MeaningfulName(string path) {
            file =  new FileStream(fileName, FileMode.Append, FileAccess.Write,
               FileShare.ReadWrite, 8192);
        }
        public void Dispose() {
            if(file != null) {
               file.Dispose();
               file = null;
            }
        }
        public void Append(byte[] buffer) {
            if(file == null) throw new ObjectDisposedException(GetType().Name);
            lock(syncLock) { // only 1 thread can be appending at a time
                file.Write(buffer, 0, buffer.Length);
                file.Flush();
            }
        }
    }
    

    That is thread-safe, and could be made available to all the ashx without issue.

    However, for larger data, you might want to look at a synchronized reader-writer queue – i.e. all the writers (ashx hits) can throw data onto the queue, with a single dedicated writer thread dequeuing them and appending. That removes the IO time from the ashx, however you might want to cap the queue size in case the writer can’t keep up. There’s a sample here of a capped synchronized reader/writer queue.

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

Sidebar

Related Questions

I need mod_rewrite functionality on an IIS .NET server. Is http://www.isapirewrite.com/ the best option?
Anybody know what the best to use to read a XmlEnumAttribute Option 1: With
What is the best option if I want to upgrade old C-code to newer
I was wondering what the best option would be for doing ajax calls to
I'm looking for the best option to store my application settings. I decided to
I´d like to know what is the best option to develop an auction site
I am trying to find the best option to create a simple progress bar
I'm starting out with MVC but not sure it's the best option. I need
given an xml string like this: <some><nested><xml>value</xml></nested></some> what's the best option (using ruby) to
Which option is better from Best practices point of view and from performance point

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.