How to do it...

  1. Create a large text file (we called ours taskFile.txt) and place it in a folder called C: emp askFile:
  1. In the AsyncDemo class, create a method called ReadBigFile() that returns a Task<TResult> type, which will be used to return an integer of bytes read from our big text file:
      public Task<int> ReadBigFile() 
{
}
  1. Add the following code to open and read the file bytes. You will see that we are using the ReadAsync() method that asynchronously reads a sequence of bytes from the stream and advances the position of that stream by the number of bytes read from that stream. You will also notice that we are using a buffer to read those bytes:
      public Task<int> ReadBigFile() 
{
var bigFile = File.OpenRead(@"C: emp askFile askFile.txt");
var bigFileBuffer = new byte[bigFile.Length];
var readBytes = bigFile.ReadAsync(bigFileBuffer, 0,
(int)bigFile.Length);

return readBytes;
}
The exceptions you can expect to handle from the ReadAsync() method are ArgumentNullException, ArgumentOutOfRangeException, ArgumentException, NotSupportedException, ObjectDisposedException, and InvalidOperatorException.
  1. Finally, add the final section of code just after the var readBytes = bigFile.ReadAsync(bigFileBuffer, 0, (int)bigFile.Length); line that uses a lambda expression to specify the work that the task needs to perform. In this case, it is to read the bytes in the file:
      public Task<int> ReadBigFile() 
{
var bigFile = File.OpenRead(@"C:temptaskFile.txt");
var bigFileBuffer = new byte[bigFile.Length];
var readBytes = bigFile.ReadAsync(bigFileBuffer, 0,
(int)bigFile.Length);
readBytes.ContinueWith(task =>
{
if (task.Status == TaskStatus.Running)
Console.WriteLine("Running");
else if (task.Status == TaskStatus.RanToCompletion)
Console.WriteLine("RanToCompletion");
else if (task.Status == TaskStatus.Faulted)
Console.WriteLine("Faulted");

bigFile.Dispose();
});
return readBytes;
}
  1. If you've not done so in the previous recipe, add a button to your Windows forms application's Forms Designer. In the winformAsync Forms Designer, open Toolbox and select the Button control, which is found under the All Windows Forms node:
  1. Drag the Button control onto the Form1 designer:
  1. With the Button control selected, double-click on the control to create the click event in the code behind. Visual Studio will insert the event code for you:
      namespace winformAsync 
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{

}
}
}
  1. Change the button1_Click event and add the async keyword to the click event. This is an example of a void returning asynchronous method:
      private async void button1_Click(object sender, EventArgs e) 
{

}
  1. Now, make sure that you add code to call the AsyncDemo class's ReadBigFile() method asynchronously. Remember to read the result from the method (which are the bytes read) into an integer variable:
      private async void button1_Click(object sender, EventArgs e) 
{
Console.WriteLine("Start file read");
AsyncDemo oAsync = new AsyncDemo();
int readResult = await oAsync.ReadBigFile();
Console.WriteLine("Bytes read = " + readResult);
}
  1. Running your application will display the Windows forms application:
  1. Before clicking on the button1 button, ensure that the Output window is visible:
  1. From the View menu, click on the Output menu item or type Ctrl +  + O to display the Output window. This will allow us to see the Console.Writeline() outputs that we added to the code in the AsyncDemo class and in the Windows application.
  2. Clicking on the button1 button will display the outputs to our Output window. Throughout this code execution, the form remains responsive:
Take note, though, that the information displayed in your Output window will differ from the screenshot. This is because the file you used is different from mine.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.200.3