In asynchronous programming, the async
methods can have three possible return types. These are:
void
Task
Task<TResult>
We will have a look at each return type in the following recipe.
What could be the use of a void
return type in asynchronous methods? Generally, void
is used with event handlers. Just bear in mind that void
returns nothing, so you can't wait for it. Therefore, if you call a void
return type asynchronous method, your calling code should be able to continue executing code without having to wait for the asynchronous method to complete.
With asynchronous methods that have a return type of Task
, you can utilize the await
operator to pause the execution of the current thread until the called asynchronous method has completed. Keep in mind that an asynchronous method that returns a type of Task
basically does not return an operand. Therefore, if it was written as a synchronous method, it would be a void
return type method. This statement might be confusing, but it will become clear in the following recipes.
Finally, asynchronous methods that have a return
statement have a return type of TResult
. In other words, if the asynchronous method returns a Boolean, you would create an asynchronous method with a return type of Task<bool>
.
Chapter6
:Class1.cs
, which we renamed to Recipes.cs
in order to distinguish the code properly. You can, however, rename your class to whatever you like, if it makes more sense to you.winformAsync
:Chapter6
class you created earlier. To do this, right-click on References under the winformAsync project and click on the Add Reference menu item from the context menu:Chapter6
class, which is found under the Projects | Solution node in the tree view to the left. Then, click on the OK button:namespace winformAsync { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { } } }
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) { }
Chapter6
class library, add a new class called AsyncDemo
:public class AsyncDemo { }
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)); }
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 return the Task
type from this asynchronous method:async Task TaskReturning_AsyncMethod() { await Task.Delay(5000); Console.WriteLine("5 second delay"); }
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(); }
private async void button1_Click(object sender, EventArgs e) { Console.WriteLine("Button Clicked"); Chapter6.AsyncDemo oAsync = new Chapter6.AsyncDemo(); await oAsync.LongTask(); Console.WriteLine("Button Click Ended"); }
Console.Writeline()
outputs as we have added them to the code in the Chapter6
class and in the Windows application.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; }
In the preceding code, we have seen the void
returning type asynchronous method that was used in the button1_Click
event. We also created a Task
returning method that returns nothing (that would be a void
if used in synchronous programming), but returning Task
type allows us to await the method. Finally, we created a Task<TResult>
returning method that performs some task and returns the result to the calling code.
18.221.126.56