Working with files asynchronously
This recipe walks us through how to create a file, and how to read and write data asynchronously.
Getting ready
To step through this recipe, you will need Visual Studio 2012. There are no other prerequisites. The source code for this recipe can be found at BookSamples\Chapter9\Recipe1
.
How to do it...
To understand how to work with files asynchronously, perform the following steps:
Start Visual Studio 2012. Create a new C# Console Application project.
In the
Program.cs
file add the followingusing
directives:using System; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks;
Add the following code snippet below the
Main
method:const int BUFFER_SIZE = 4096; async static Task ProcessAsynchronousIO() { using (var stream = new FileStream("test1.txt", FileMode.Create, FileAccess.ReadWrite, FileShare.None, BUFFER_SIZE)) { Console.WriteLine("1. Uses I/O Threads: {0}", stream.IsAsync); byte[] buffer = Encoding.UTF8.GetBytes...