- Overview of Asynchrony
- The Old-Fashioned Way: Event-Based Asynchrony
- The Old-Fashioned Way: The Asynchronous Programming Model
- The Modern Way: The Async Pattern
- Getting Started with Async/ Await
- Exception Handling in Async
- Implementing Task-Based Asynchrony
- Cancellation and Progress
- Asynchronous Lambda Expressions
- Asynchronous I/O File Operations in .NET 4.6
- Debugging Tasks
- Summary
Asynchronous I/O File Operations in .NET 4.6
Before .NET Framework 4.5, you could perform asynchronous operations over files and streams by using the Asynchronous Programming Model and methods such as Stream.BeginRead and Stream.EndRead. This kind of approach can be good, but it has the limitations described in the section “Getting Started with Async/Await” in this chapter. With .NET Framework 4.5 and after, asynchronous I/O operations can be simplified by using the Async pattern and by implementing asynchronous versions of methods that work with files and stream to avoid blocking the main thread. Such methods are exposed by the Stream, FileStream, MemoryStream, TextReader, and TextWriter classes that you saw in action back in Chapter 18, “Manipulating Files and Streams.” Table 42.1 summarizes the available asynchronous methods.
TABLE 42.1 Asynchronous Methods for Stream Classes
Method |
Description |
Return Type |
ReadAsync |
Reads a sequence of bytes from a stream and advances the position by the number of bytes read, with an asynchronous approach |
Task(Of Integer) |
WriteAsync |
Writes a sequence of bytes to a stream, with an asynchro-nous approach |
Task |
FlushAsync |
Clears buffers associated with the stream sending buffered data to the stream, using asynchrony |
Task |
CopyToAsync |
Asynchronously copies a number of bytes from a stream to another |
Task |
ReadLineAsync |
Reads a line of characters using asynchrony and returns a string |
Task(Of String) |
ReadToEndAsync |
Asynchronously reads all characters from the current posi-tion to the end of the stream, and returns one string |
Task(Of String) |
To see some of these methods in action, create a new WPF project. The user interface of this sample application will have to look like Figure 42.7.
FIGURE 42.7 The user interface of the new sample application.
That said, add the following controls in the designer:
- Three buttons, named ReadTextButton, CopyButton, and WriteButton. Then, set their Content properties with Read text, Copy files, and Write Something, respectively.
- Four TextBox controls, named ReadTextBox, SourceTextBox, DestinationTextBox, and StatusTextBox.
- Three TextBlock controls. You do not need to specify a name, but make sure their Text property is set with Source folder, Destination folder, and Status, respectively.
The first example uses the StreamReader class to read a text file asynchronously. The event handler for the ReadTextButton.Click event looks like this:
Private Async Sub ReadTextButton_Click(sender As Object, e As RoutedEventArgs) _ Handles ReadTextButton.Click Using reader As New StreamReader("TextFile1.txt") Me.ReadTextBox.Text = Await reader.ReadToEndAsync End Using End Sub
You mark the event handler with Async, and because this method will not be awaited by any other methods, it does not need to return a Task. Therefore, it can be defined as a Sub. Notice how you use Await together with the ReadToEndAsync method, while the rest of the implementation is made the usual way. The next example is about copying streams asynchronously. The following code shows the implementation of the CopyButton.Click event handler:
Private Async Sub CopyButton_Click(sender As Object, e As RoutedEventArgs) _ Handles CopyButton.Click If Me.SourceTextBox.Text = "" Then MessageBox.Show("Please specify the source folder") Exit Sub End If If Me.DestinationTextBox.Text = "" Then MessageBox.Show("Please specify the target folder") Exit Sub End If For Each fileName As String In Directory. EnumerateFiles(Me.SourceTextBox.Text) Using SourceStream As FileStream = File.Open(fileName, FileMode.Open) Using DestinationStream As FileStream = File.Create(String.Concat(Me.DestinationTextBox.Text, fileName. Substring(fileName.LastIndexOf("\"c)))) Await SourceStream.CopyToAsync(DestinationStream) Me.StatusTextBox.Text = "Copied " + DestinationStream.Name End Using End Using Next End Sub
In particular, the code enumerates the content of the source folder and for each file it opens a stream for reading and another one for writing into the target folder. Await enables you to execute asynchronously the operation with the asynchronous method called CopyToAsync. It is worth mentioning that, with this approach, you can refresh the user interface with useful information, like showing the name of the last copied file. In a synchronous approach, this would not be possible because the UI would be blocked until the completion of the operation. The last example demonstrates how to write some text into a file asynchronously. This is the event handler for the WriteButton.Click event:
Private Async Sub WriteButton_Click(sender As Object, e As RoutedEventArgs) Handles WriteButton.Click Dim uniencoding As UnicodeEncoding = New UnicodeEncoding() Dim filename As String = "c:\temp\AsyncWriteDemo.txt" Dim result As Byte() = uniencoding.GetBytes("Demo for Async I/O") Using SourceStream As FileStream = File.Open(filename, FileMode.OpenOrCreate) SourceStream.Seek(0, SeekOrigin.End) Await SourceStream.WriteAsync(result, 0, result.Length) End Using End Sub
In this particular example, the code is written exactly like in the synchronous counterpart, except for the Async modifier and the Await statement that invokes the WriteAsync method (instead of running Write). By using the Async pattern, writing applications that work with streams and remain responsive has become dramatically simpler.