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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:47:45+00:00 2026-06-14T02:47:45+00:00

Why does creating a FileStream with FileOptions.Asynchronous cause FileStream.BeginRead to block the calling thread?

  • 0

Why does creating a FileStream with FileOptions.Asynchronous cause FileStream.BeginRead to block the calling thread?

Here is the code snippet:

private static Task<int> ReadFileAsync(string filePath)
  {
     var file = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite, 64 * 1024, FileOptions.Asynchronous);
     FileInfo fi = new FileInfo(filePath);
     byte[] buffer = new byte[fi.Length];
     Task<int> task = Task<int>.Factory.FromAsync(file.BeginRead, file.EndRead, buffer, 0, buffer.Length, null);         
     return task.ContinueWith(t =>
     {
        file.Close();
        Console.WriteLine("Done ReadFileAsync, read " + t.Result + " bytes.");
        return t.Result;
     });
  }

When digging around in the MSFT FileStream code using JetBrains dotPeek it seems like there is a bug in their code:

      if (!this._isAsync)
    return base.BeginRead(array, offset, numBytes, userCallback, stateObject);
  else
    return (IAsyncResult) this.BeginReadAsync(array, offset, numBytes, userCallback, stateObject);

The BeginRead method actually seems to do reading asynchronously by scheduling a Task, but the BeginReadAsync method actually ends up doing a synchronous read.
So their method naming nomenclature is backwards and the logic of which method is called is wrong. BeginRead should be called if this._isAsync == true.

So it seems that to get FileStream.BeginRead to return right away (asynchronously schedule a read) you actually have to set the useAsync parameter in the cosntructor to false.

  • 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-14T02:47:47+00:00Added an answer on June 14, 2026 at 2:47 am

    Here’s a knowledge base article that lists all the ways that can cause code you desire to be executed asynchronously to actually run synchronously.

    Asynchronous Disk I/O Appears as Synchronous on Windows NT, Windows 2000, and Windows XP

    Does anything on the list apply to your situation?

    Have you tried implementing the same behavior with .NET 4.5’s ReadAsync method?

    I am quoting from MSDN:

    In the .NET Framework 4 and earlier versions, you have to use methods such as BeginRead and EndRead to implement asynchronous I/O operations. These methods are still available in the .NET Framework 4.5 to support legacy code; however, the new async methods, such as ReadAsync, WriteAsync, CopyToAsync, and FlushAsync, help you implement asynchronous I/O operations more easily.

    EDIT I am reproducing your issue with a 256MB file on OCZ Vertex 2 with ICH10 and Windows 7. I am having to generate the file, reboot the PC to clear file cache, and then try and read the same file.

    using System;
    using System.Threading.Tasks;
    using System.IO;
    using System.Diagnostics;
    using System.Threading;
    
    namespace ConsoleApplication4
    {
       class Program
       {
          static void Main(string[] args)
          {
             string fileName = @"C:\Temp\a1.txt";
             int arraySize = 512 * 1024 * 1024;
             var bytes = new byte[arraySize];
             new Random().NextBytes(bytes);
    
              // This prints false, as expected for async call
             var callback = new AsyncCallback(result =>
                                 Console.WriteLine("Completed Synchronously: " + result.CompletedSynchronously));
    
             try
             {
                // Use this method to generate file...
                //WriteFileWithRandomBytes(fileName, arraySize, bytes, callback);
    
                Console.WriteLine("ReadFileAsync invoked at " + DateTimeOffset.Now);
                var task = ReadFileAsync(fileName);
                Console.WriteLine("ReadFileAsync completed at " + DateTimeOffset.Now);
    
                Task.WaitAll(task);
                Console.WriteLine("Wait on a read task completed at " + DateTimeOffset.Now);
             }
             finally
             {
                if (File.Exists(fileName))
                   File.Delete(fileName);
             }
          }
    
          private static void WriteFileWithRandomBytes(string fileName, int arraySize, byte[] bytes, AsyncCallback callback)
          {
             using (var fileStream = new FileStream(fileName,
                FileMode.OpenOrCreate, FileAccess.Write, FileShare.None, 128 * 1024, FileOptions.Asynchronous))
             {
                Console.WriteLine("BeginWrite invoked at " + DateTimeOffset.Now);
                var asyncResult = fileStream.BeginWrite(bytes, 0, arraySize, callback, null);
    
    
                Console.WriteLine("BeginWrite completed at " + DateTimeOffset.Now);
                // completes in 6 seconds or so...  Expecting instantaneous return instead of blocking
    
                // I expect runtime to block here...
                Task.WaitAll(Task.Factory.FromAsync(asyncResult, fileStream.EndWrite));
    
                // or at least when flushing the stream on the following end-curly
             }
          }
    
    
          private static Task<int> ReadFileAsync(string filePath)
          {
             FileInfo fi = new FileInfo(filePath);
             byte[] buffer = new byte[fi.Length];
    
             var file = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.None, 64 * 1024, FileOptions.Asynchronous);
             Task<int> task = Task<int>.Factory.FromAsync(file.BeginRead, file.EndRead, buffer, 0, buffer.Length, null);
             return task.ContinueWith(t =>
             {
                file.Close();
                Console.WriteLine("Done ReadFileAsync, read " + t.Result + " bytes.");
                return t.Result;
             });
          }
       }
    }
    

    When all else fails, here’s the reference to unmanaged API documentation.

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

Sidebar

Related Questions

this module does a good job at creating a tagcloud block - all good
Please, look into this code. Why does creating of the same regular expression by
Does creating an object using reflection rather than calling the class constructor result in
Does creating a lot of private variables that may never be used increase file
Firefox 3.5 does not allow creating java OBJECT tag with Javascript (document.write)? this technique
Why does this attempt at creating a list of curried functions not work? def
does Android have a best practices guideline on creating & populating the db/tables programmatically
What does one loose by creating POCO using T4 templates in entity framework 4.0?
Does anyone have a hello world sample or tutorial for creating an Eclipse plugin
I am creating a flash object that does some heavy image lifting. What 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.