async and await in console app - c#

Given this is the code below I'd expect to see this output:
----- Enter main
----- Exit main
----- 185ms passed
But instead, it acts synchronously and Foo blocks MainAsync from returning. Can anyone clarify?
class Program
{
static void Main(string[] args)
{
MainAsync().GetAwaiter().GetResult();
}
static async Task MainAsync()
{
Console.WriteLine("----- Enter main");
await Foo();
Console.WriteLine("----- Exit main");
Console.ReadKey();
}
public static async Task Foo()
{
DateTime time = DateTime.Now;
HttpClient client = new HttpClient();
string urlContents = await client.GetStringAsync("http://msdn.microsoft.com");
Console.WriteLine("----- " + (DateTime.Now - time).Milliseconds + "ms passed");
}
}

await Foo(); means that the rest of the method won't run until after the Task returned by Foo has completed. That's what it means to await something; the method won't continue until that Task has completed. As such, Console.WriteLine("----- Exit main"); won't run until after Foo has completed, which won't be until after it has already written out the time it took.

Related

console application and async await example is blocking

Why is the following example blocking? I was expecting "Run First" to run immediately and then 5 seconds later the "Run Last" would appear.
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// Call SomeAsyncCode() somehow
SomeAsyncCode().GetAwaiter().GetResult();
Console.WriteLine("Run First");
}
private static async Task SomeAsyncCode()
{
// Use await here!
await Task.Delay(5000);
Console.WriteLine("Run Last");
// Other async goodness...
}
}
}
You are awaiting (term used loosely) the task.
It would make more sense if you didn't use .GetAwaiter().GetResult(); (which are internal framework methods anyway) and just wrote
SomeAsyncCode().Wait();
Console.WriteLine("Run First");
It then becomes obvious whats happening, you are waiting for the task to complete.
The following is probably more like what you expect;
// start task
var task = SomeAsyncCode();
Console.WriteLine("Run First");
task.Wait();
In all honesty though, it should be rare to need to call, Wait, Result or GetAwaiter().GetResult() on an async method, doing so in a UI app, or something with a Synchronization Context will likely cause a deadlock
Ideally you let async and await propagate, in C# 7.1 and higher you have the ability to create an async Entry Point which allows you use the Async and Await Pattern in a more succinct manner
static async Task Main(string[] args)
{
await SomeAsyncCode();
Console.WriteLine("Run First");
}
or
static async Task Main(string[] args)
{
var task = SomeAsyncCode();
Console.WriteLine("Run First");
await Task.WhenAll(task);
}
When you define async Task Method() - you are telling that this method should be awaited even though it will return nothing.
When you define async void Method() - you are telling that this is fire'n'forget method and you don't want to await it.
Also, when you void Main(string[] args) is returning - your application is closing and GC will kill all your tasks.
So, your code should be more like this:
static void Main(string[] args)
{
// Call SomeAsyncCode() somehow
SomeAsyncCode();
Console.WriteLine("Run First");
Console.ReadKey(); // you need this to prevent app from closing
}
private static async void SomeAsyncCode()
{
// Use await here!
await Task.Delay(5000);
Console.WriteLine("Run Last");
// Other async goodness...
}

Application hangs while running async task

I am learning about async/await and created a dummy console application. When I try to get the result from async method the program just hangs. Any idea what is wrong in the following code.
static void Main(string[] args)
{
var task = Task.Factory.StartNew(() => 5);
var x = TestAsync();
//x.Start();
Console.WriteLine(x.Result);
}
private static Task<int> CalculateValue()
{
Console.WriteLine("In CalculateValue"); // This line is printed.
Task<int> t = new Task<int>(GetValue); // The program hangs here.
return t;
}
public static async Task<int> TestAsync()
{
int result = await CalculateValue();
return result;
}
private static int GetValue()
{
return 10;
}
First of all:
Task<int> t = new Task<int>(GetValue); // The program hangs here.
is incorrect, the program actually hangs here :
Console.WriteLine(x.Result);
.Result blocks current thread until task x completes execution and returns result. It never completes as it awaits task returned by CalculateValue method whis is this task:
Task<int> t = new Task<int>(GetValue);
This is so called 'cold Task' which means that it's a task in an inactive state.
To start a 'hot' task (which basically means start the task) use the Task.Run method:
Task<int> t = Task.Run(GetValue);

async Main wouldn't compile [duplicate]

I copied below code from this link.But when I am compiling this code I am getting an entry point cannot be marked with the 'async' modifier. How can I make this code compilable?
class Program
{
static async void Main(string[] args)
{
Task<string> getWebPageTask = GetWebPageAsync("http://msdn.microsoft.com");
Debug.WriteLine("In startButton_Click before await");
string webText = await getWebPageTask;
Debug.WriteLine("Characters received: " + webText.Length.ToString());
}
private static async Task<string> GetWebPageAsync(string url)
{
// Start an async task.
Task<string> getStringTask = (new HttpClient()).GetStringAsync(url);
// Await the task. This is what happens:
// 1. Execution immediately returns to the calling method, returning a
// different task from the task created in the previous statement.
// Execution in this method is suspended.
// 2. When the task created in the previous statement completes, the
// result from the GetStringAsync method is produced by the Await
// statement, and execution continues within this method.
Debug.WriteLine("In GetWebPageAsync before await");
string webText = await getStringTask;
Debug.WriteLine("In GetWebPageAsync after await");
return webText;
}
// Output:
// In GetWebPageAsync before await
// In startButton_Click before await
// In GetWebPageAsync after await
// Characters received: 44306
}
The error message is exactly right: the Main() method cannot be async, because when Main() returns, the application usually ends.
If you want to make a console application that uses async, a simple solution is to create an async version of Main() and synchronously Wait() on that from the real Main():
static void Main()
{
MainAsync().Wait();
}
static async Task MainAsync()
{
// your async code here
}
This is one of the rare cases where mixing await and Wait() is a good idea, you shouldn't usually do that.
Update: Async Main is supported in C# 7.1.
Starting from C# 7.1 there are 4 new signatures for Main method which allow to make it async(Source, Source 2, Source 3):
public static Task Main();
public static Task<int> Main();
public static Task Main(string[] args);
public static Task<int> Main(string[] args);
You can mark your Main method with async keyword and use await inside Main:
static async Task Main(string[] args)
{
Task<string> getWebPageTask = GetWebPageAsync("http://msdn.microsoft.com");
Debug.WriteLine("In startButton_Click before await");
string webText = await getWebPageTask;
Debug.WriteLine("Characters received: " + webText.Length.ToString());
}
C# 7.1 is available in Visual Studio 2017 15.3.
I'm using C# 8 and its working fine.
static async Task Main(string[] args)
{
var response = await SomeAsyncFunc();
Console.WriteLine("Async response", response);
}
OR without "await" keyword.
static void Main(string[] args)
{
var response = SomeAsyncFunc().GetAwaiter().GetResult();
Console.WriteLine("Async response", response);
}
The difference between the code in the link's example and yours, is that you're trying to mark the Main() method with an async modifier - this is not allowed, and the error says that exactly - the Main() method is the "entry point" to the application (it's the method that is executed when your application starts), and it's not allowed to be async.
Wrap your async code in MainAsync() - which is an async function
then call MainAsync().GetAwaiter().GetResult();

When async-await change thread?

When I call test1() method then ManagedThreadId will be changed after
Delay(1).
When I call test2() method (instead of test1()) then ManagedThreadId
will stay same.
When async-await change thread?
In test2() method time required to finish method is even longer then in test1()
[Route("api/[controller]")]
[HttpGet]
public async Task<IActionResult> Get()
{
await MainAsync();
return new ObjectResult(null);
}
static async Task MainAsync()
{
Console.WriteLine("Main Async: " + System.Threading.Thread.CurrentThread.ManagedThreadId.ToString());
await test1();
//await test2();
// . . .more code
}
private static async Task test1()
{
Console.WriteLine("thisIsAsyncStart: " + System.Threading.Thread.CurrentThread.ManagedThreadId.ToString());
await Task.Delay(1);
Console.WriteLine("thisIsAsyncEnd: " + System.Threading.Thread.CurrentThread.ManagedThreadId.ToString());
}
private static async Task test2()
{
Console.WriteLine("thisIsAsyncStart: " + System.Threading.Thread.CurrentThread.ManagedThreadId.ToString());
System.Threading.Thread.Sleep(5000);
await Task.FromResult(0);
Console.WriteLine("thisIsAsyncEnd: " + System.Threading.Thread.CurrentThread.ManagedThreadId.ToString());
}
test1 awaits Task.Delay(1), which isn't going to be completed at the time it goes to await it, meaning the rest of test1 needs to be scheduled as a continuation.
For test2 you're awaiting Task.FromResult, which will always return an already completed Task. When you await an already completed task the method can just keep running on the current thread, without needing to schedule a continuation.

Understanding async / await in C#

I'm starting to learn about async / await in C# 5.0, and I don't understand it at all. I don't understand how it can be used for parallelism. I've tried the following very basic program:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Task task1 = Task1();
Task task2 = Task2();
Task.WaitAll(task1, task2);
Debug.WriteLine("Finished main method");
}
public static async Task Task1()
{
await new Task(() => Thread.Sleep(TimeSpan.FromSeconds(5)));
Debug.WriteLine("Finished Task1");
}
public static async Task Task2()
{
await new Task(() => Thread.Sleep(TimeSpan.FromSeconds(10)));
Debug.WriteLine("Finished Task2");
}
}
}
This program just blocks on the call to Task.WaitAll() and never finishes, but I am not understanding why. I'm sure I'm just missing something simple or just don't have the right mental model of this, and none of the blogs or MSDN articles that are out there are helping.
I recommend you start out with my intro to async/await and follow-up with the official Microsoft documentation on TAP.
As I mention in my intro blog post, there are several Task members that are holdovers from the TPL and have no use in pure async code. new Task and Task.Start should be replaced with Task.Run (or TaskFactory.StartNew). Similarly, Thread.Sleep should be replaced with Task.Delay.
Finally, I recommend that you do not use Task.WaitAll; your Console app should just Wait on a single Task which uses Task.WhenAll. With all these changes, your code would look like:
class Program
{
static void Main(string[] args)
{
MainAsync().Wait();
}
public static async Task MainAsync()
{
Task task1 = Task1();
Task task2 = Task2();
await Task.WhenAll(task1, task2);
Debug.WriteLine("Finished main method");
}
public static async Task Task1()
{
await Task.Delay(5000);
Debug.WriteLine("Finished Task1");
}
public static async Task Task2()
{
await Task.Delay(10000);
Debug.WriteLine("Finished Task2");
}
}
Understand C# Task, async and await
C# Task
Task class is an asynchronous task wrapper. Thread.Sleep(1000) can stop a thread running for 1 second. While Task.Delay(1000) won't stop the current work. See code:
public static void Main(string[] args){
TaskTest();
}
private static void TaskTest(){
Task.Delay(5000);
System.Console.WriteLine("task done");
}
When running," task done" will show up immediately. So I can assume that every method from Task should be asynchronous. If I replace TaskTest () with Task.Run(() =>TaskTest()) task done won't show up at all until I append a Console.ReadLine(); after the Run method.
Internally, Task class represent a thread state In a State Machine. Every state in state machine have several states such as Start, Delay, Cancel, and Stop.
async and await
Now, you may wondering if all Task is asynchronous, what is the purpose of Task.Delay ? next, let's really delay the running thread by using async and await
public static void Main(string[] args){
TaskTest();
System.Console.WriteLine("main thread is not blocked");
Console.ReadLine();
}
private static async void TaskTest(){
await Task.Delay(5000);
System.Console.WriteLine("task done");
}
async tell caller, I am an asynchronous method, don't wait for me. await inside the TaskTest() ask for waiting for the asynchronous task. Now, after running, program will wait 5 seconds to show the task done text.
Cancel a Task
Since Task is a state machine, there must be a way to cancel the task while task is in running.
static CancellationTokenSource tokenSource = new CancellationTokenSource();
public static void Main(string[] args){
TaskTest();
System.Console.WriteLine("main thread is not blocked");
var input=Console.ReadLine();
if(input=="stop"){
tokenSource.Cancel();
System.Console.WriteLine("task stopped");
}
Console.ReadLine();
}
private static async void TaskTest(){
try{
await Task.Delay(5000,tokenSource.Token);
}catch(TaskCanceledException e){
//cancel task will throw out a exception, just catch it, do nothing.
}
System.Console.WriteLine("task done");
}
Now, when the program is in running, you can input "stop" to cancel the Delay task.
Your tasks never finish because they never start running.
I would Task.Factory.StartNew to create a task and start it.
public static async Task Task1()
{
await Task.Factory.StartNew(() => Thread.Sleep(TimeSpan.FromSeconds(5)));
Debug.WriteLine("Finished Task1");
}
public static async Task Task2()
{
await Task.Factory.StartNew(() => Thread.Sleep(TimeSpan.FromSeconds(10)));
Debug.WriteLine("Finished Task2");
}
As a side note, if you're really just trying to pause in a async method, there's no need to block an entire thread, just use Task.Delay
public static async Task Task1()
{
await Task.Delay(TimeSpan.FromSeconds(5));
Debug.WriteLine("Finished Task1");
}
public static async Task Task2()
{
await Task.Delay(TimeSpan.FromSeconds(10));
Debug.WriteLine("Finished Task2");
}
Async and await are markers which mark code positions from where control should resume after a task (thread) completes.
Here's a detail youtube video which explains the concept in a demonstrative manner http://www.youtube.com/watch?v=V2sMXJnDEjM
If you want you can also read this coodeproject article which explains the same in a more visual manner.
http://www.codeproject.com/Articles/599756/Five-Great-NET-Framework-4-5-Features#Feature1:-“Async”and“Await”(Codemarkers)
static void Main(string[] args)
{
if (Thread.CurrentThread.Name == null)
Thread.CurrentThread.Name = "Main";
Console.WriteLine(Thread.CurrentThread.Name + "1");
TaskTest();
Console.WriteLine(Thread.CurrentThread.Name + "2");
Console.ReadLine();
}
private async static void TaskTest()
{
Console.WriteLine(Thread.CurrentThread.Name + "3");
await Task.Delay(2000);
if (Thread.CurrentThread.Name == null)
Thread.CurrentThread.Name = "FirstTask";
Console.WriteLine(Thread.CurrentThread.Name + "4");
await Task.Delay(2000);
if (Thread.CurrentThread.Name == null)
Thread.CurrentThread.Name = "SecondTask";
Console.WriteLine(Thread.CurrentThread.Name + "5");
}
If you run this program you will see that await will use different thread. Output:
Main1
Main3
Main2
FirstTask4 // 2 seconds delay
SecondTask5 // 4 seconds delay
But if we remove both await keywords, you will learn that async alone doesn't do much. Output:
Main1
Main3
Main4
Main5
Main2

Categories