From 8619882af7cb462249db880aeae6d2c2c24e939f Mon Sep 17 00:00:00 2001 From: mattwar Date: Thu, 16 Oct 2014 13:41:14 -0700 Subject: [PATCH] Change FileTextLoader to open FileStream for asynchronous IO. (changeset 1355769) --- Src/Compilers/Core/Desktop/FileUtilities.cs | 18 ++++++++++++++++++ .../Core/Desktop/Workspace/FileTextLoader.cs | 3 ++- .../MSBuild/ProjectFile/ProjectFileLoader.cs | 2 +- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/Src/Compilers/Core/Desktop/FileUtilities.cs b/Src/Compilers/Core/Desktop/FileUtilities.cs index 32d7ab32516..abd12af610a 100644 --- a/Src/Compilers/Core/Desktop/FileUtilities.cs +++ b/Src/Compilers/Core/Desktop/FileUtilities.cs @@ -296,6 +296,24 @@ internal static FileStream OpenRead(string fullPath) } } + internal static FileStream OpenAsyncRead(string fullPath) + { + Debug.Assert(PathUtilities.IsAbsolute(fullPath)); + + try + { + return new FileStream(fullPath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.Asynchronous); + } + catch (IOException) + { + throw; + } + catch (Exception e) + { + throw new IOException(e.Message, e); + } + } + /// /// Used to create a file given a path specified by the user. /// paramName - Provided by the Public surface APIs to have a clearer message. Internal API just rethrow the exception diff --git a/Src/Workspaces/Core/Desktop/Workspace/FileTextLoader.cs b/Src/Workspaces/Core/Desktop/Workspace/FileTextLoader.cs index 9c275ebaff7..9cfc8700d51 100644 --- a/Src/Workspaces/Core/Desktop/Workspace/FileTextLoader.cs +++ b/Src/Workspaces/Core/Desktop/Workspace/FileTextLoader.cs @@ -70,8 +70,9 @@ public override async Task LoadTextAndVersionAsync(Workspace wor // Allowing other theads/processes to write or delete the file is essential for scenarios such as // Rename refactoring where File.Replace API is invoked for updating the modified file. TextAndVersion textAndVersion; - using (var stream = FileUtilities.OpenRead(this.path)) + using (var stream = FileUtilities.OpenAsyncRead(this.path)) { + System.Diagnostics.Debug.Assert(stream.IsAsync); var version = VersionStamp.Create(prevLastWriteTime); var memoryStream = await this.ReadStreamAsync(stream, cancellationToken: cancellationToken).ConfigureAwait(false); diff --git a/Src/Workspaces/Core/Desktop/Workspace/MSBuild/ProjectFile/ProjectFileLoader.cs b/Src/Workspaces/Core/Desktop/Workspace/MSBuild/ProjectFile/ProjectFileLoader.cs index 41df3711bc3..f73c2f0921c 100644 --- a/Src/Workspaces/Core/Desktop/Workspace/MSBuild/ProjectFile/ProjectFileLoader.cs +++ b/Src/Workspaces/Core/Desktop/Workspace/MSBuild/ProjectFile/ProjectFileLoader.cs @@ -70,7 +70,7 @@ private static async Task ReadFileAsync(string path, CancellationT { MemoryStream memoryStream = new MemoryStream(); var buffer = new byte[1024]; - using (var stream = FileUtilities.OpenRead(path)) + using (var stream = FileUtilities.OpenAsyncRead(path)) { int bytesRead = 0; do -- GitLab