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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:14:31+00:00 2026-05-26T02:14:31+00:00

I need to make a batch of writes to the same file but at

  • 0

I need to make a batch of writes to the same file but at different places within the file. I want to achieve this with the best performance possible and so have looked at the synchronous FileStream.Write and asynchronous FileStream.BeginWrite methods.

A synchronous implemention is trivial and simply calls the FileStream.Write the required number of times in a loop. The async version calls FileStream.BeginWrite in a loop and then does a WaitHandle.WaitAll in order to block until they have all completed. To my surprise this works slower than the simple synchronous version.

I have created the FileStream using the correct constructor so I can request async operation and I also tested the IAsyncResult.CompletedSynchronous property which indicated False and so they did indeed operate in an async fashion. It seems that the only benefit from using BeginWrite is that you are not blocking your thread whilst the write occurs. Apart from this benefit is there any point in using the async version?

Here is the test code I used for playing with the async method, maybe there is an obvious error?

        // Size of a chunk to be written to file
        var chunk = 1024 * 64;

        // Number of chunks to write async
        var reps = 32;

        // Create new file and set length
        var fs = new FileStream(@"C:\testfile.dat", 
                                FileMode.Create, FileAccess.ReadWrite, 
                                FileShare.None, chunk, true);
        fs.SetLength(chunk * reps);

        // Allocate resources
        byte[] bytes = new byte[chunk];
        WaitHandle[] handles = new WaitHandle[reps];

        for (int i = 0; i < reps; i++)
        {
            fs.Seek(chunk * i, SeekOrigin.Begin);
            handles[i] = fs.BeginWrite(bytes, 0, chunk, null, null).AsyncWaitHandle;
        }

        // Wait for all async operations to complete
        WaitHandle.WaitAll(handles);

        fs.Flush();
        fs.Close();
  • 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-26T02:14:31+00:00Added an answer on May 26, 2026 at 2:14 am

    File writes are heavily optimized in Windows. You don’t actually write to the disk, you write to the file system cache. A memory-to-memory copy, runs at 5 gigabytes per second or better. From the cache, the data is then lazily written to disk. In turn optimized to minimize the number of write head moves.

    This is next to impossible to optimize with asynchronous writes. Which do indeed take longer, grabbing the threadpool thread to make the callback doesn’t come for free. The benefit of async here is minimizing the main thread delays, not to actually make it more efficient. You will only actually get the benefit when you write very large amounts of data. More than will fit into the cache. At that point, write perf will fall off a cliff from 5 GB/sec to less than ~50 MB/sec since cache space can only become available at the rate the disk can be written.

    Exactly when that happens is hard to predict. It depends how much RAM the machine has and how much of it is needed by other processes. You basically don’t worry about it when you write a gigabyte or less. And it is important that you actually have something useful to do when the async write is pending. Waiting for them to complete defeats the point of using it.

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

Sidebar

Related Questions

I need to make a batch file that can copy files from one path
I need to make this function work everywhere, except IE6-7 $(function(){ window.scrollTo(0,300); }) Please
I need to make a cURL request to a https URL, but I have
I need to write a batch file to unzip files to their current folder
I have a large xml file (1Gb). I need to make many queries on
I need to modify batch file. I have a batch file that opens many
I have a template file (say myTemplate.txt) and I need to make some edits
I need to add a batch line in my script that make me the
I need to copy multiple files in a single batch file. The files have
I was wondering how to make a batch file that runs a certain command/commands

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.