How to do it...

  1. Create a new Windows forms project in Visual Studio called winformAsync. We will be creating a new Windows forms application so that we can create a button-click event. 
  1. On the winformAsync Forms Designer, openToolbox 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 an asynchronous method:
      private async void button1_Click(object sender, EventArgs e) 
{
}
  1. Next, create a new class called AsyncDemo:
      public class AsyncDemo 
{
}
  1. The next method to add to the AsyncDemo class is the asynchronous method that returns TResult (in this case, a boolean). This method simply checks whether the current year is a leap year. It then returns a boolean to the calling code:
      async Task<bool> TaskOfTResultReturning_AsyncMethod() 
{
return await Task.FromResult<bool>
(DateTime.IsLeapYear(DateTime.Now.Year));
}
  1. The next method to add is the void returning method that returns a Task type so that it allows you to await the method. The method itself does not return any result, making it a void returning method. However, in order to use the await keyword, you to return the Task type from this asynchronous method:
      async Task TaskReturning_AsyncMethod() 
{
await Task.Delay(5000);
Console.WriteLine("5 second delay");
}
  1. Finally, add a method that will call the previous asynchronous methods and display the result of the leap year check. You will notice that we are using the await keyword with both method calls:
      public async Task LongTask() 
{
bool isLeapYear = await TaskOfTResultReturning_AsyncMethod();
Console.WriteLine($"{DateTime.Now.Year} {(isLeapYear ? " is " :
" is not ")} a leap year");
await TaskReturning_AsyncMethod();
}
  1. In the button click, add the following code that calls the long-running task asynchronously:
      private async void button1_Click(object sender, EventArgs e) 
{
Console.WriteLine("Button Clicked");
AsyncDemo oAsync = new AsyncDemo();
await oAsync.LongTask();
Console.WriteLine("Button Click Ended");
}
  1. Running your application will display the Windows forms application:
  1. Before clicking on the button1 button, ensure that the Output window is visible. To do this, click on View and then Output. You can also just hold down Ctrl + WO.
  1. Displaying the Output window 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:
  1. Finally, you can also use the await operator in separate calls. Modify the code in the LongTask() method as follows:
      public async Task LongTask() 
{
Task<bool> blnIsLeapYear = TaskOfTResultReturning_AsyncMethod();

for (int i = 0; i <= 10000; i++)
{
// Do other work that does not rely on
// blnIsLeapYear before awaiting
}

bool isLeapYear = await TaskOfTResultReturning_AsyncMethod();
Console.WriteLine($"{DateTime.Now.Year} {(isLeapYear ?
" is " : " is not ")} a leap year");

Task taskReturnMethhod = TaskReturning_AsyncMethod();

for (int i = 0; i <= 10000; i++)
{
// Do other work that does not rely on
// taskReturnMethhod before awaiting
}

await taskReturnMethhod;
}
..................Content has been hidden....................

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