var getLeisureHoursTask = GetLeisureHoursAsync() The previous example retrieved the value of the Result property to block the main thread so that the Main method could print the message to the console before the application ended. In most cases, you should access the value by using await instead of accessing the property directly. If you try to access it before its task is finished, the thread that's currently active is blocked until the task completes and the value is available. The Result property is a blocking property. The value is assigned to the ret variable. When await is applied to getLeisureHoursTask, the await expression evaluates to the contents of the Result property of getLeisureHoursTask. In this case, TResult represents an integer type. Because getLeisureHoursTask is a Task, it contains a Result property of type TResult. The task is assigned to the getLeisureHoursTask variable in the example. A call to method GetLeisureHoursAsync that isn't immediately awaited returns a Task, as you would expect from the declaration of the method. You can better understand how await retrieves the result from a Task by separating the call to GetLeisureHoursAsync from the application of await, as the following code shows. For more information about await expressions, see await. When GetLeisureHoursAsync is called from within an await expression in the ShowTodaysInfo method, the await expression retrieves the integer value (the value of leisureHours) that's stored in the task returned by the GetLeisureHours method. Today is DayOfWeek.Saturday || today is DayOfWeek.Sunday public static async Task ShowTodaysInfoAsync()ĭayOfWeek today = await Task.FromResult() The FromResult async method is a placeholder for an operation that returns a DayOfWeek. The method declaration must specify a return type of Task. In the following example, the GetLeisureHoursAsync method contains a return statement that returns an integer. The Task return type is used for an async method that contains a return statement in which the operand is TResult. "The current temperature is 76 degrees.\n" public static async Task Displa圜urrentInfoAsync()Ĭonsole.WriteLine($"Today is \n" The Task type doesn't include a Result property because it has no return value. Returning a Task enables WaitAndApologizeAsync to be awaited. In the following example, the WaitAndApologizeAsync method doesn't contain a return statement, so the method returns a Task object. If you use a Task return type for an async method, a calling method can use an await operator to suspend the caller's completion until the called async method has finished. Such methods return void if they run synchronously. IAsyncOperationWithProgress, for async operations in UWP that report progress and return a value.Īsync methods that don't contain a return statement or that contain a return statement that doesn't return an operand usually have a return type of Task.IAsyncOperation, for async operations in UWP that return a value.IAsyncActionWithProgress, for async actions in UWP that report progress but don't return a value.IAsyncAction, for async actions in UWP that don't return a value.DispatcherOperation, for async operations limited to Windows.Several other types also exist that are specific to Windows workloads: IAsyncEnumerable, for an async method that returns an async stream.įor more information about async methods, see Asynchronous programming with async and await (C#).The object returned by the GetAwaiter method must implement the 圜ompletion interface. Any type that has an accessible GetAwaiter method.Task, for an async method that returns a value.Task, for an async method that performs an operation but returns no value.Async methods can have the following return types:
0 Comments
Leave a Reply. |