Click here to Skip to main content
15,881,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am calling RunTest() method to execute the test.
I am expecting, it should print..
"AsyncAndAwait starting..."
"Doing something else..."
and then it should wait for some time.. and then it should print "I am done!!!"

But it not doing so.. The actual results are.
"AsyncAndAwait starting..."
Wait for some time
"Doing something else..."
"I am done!!!"

Please help me understanding how is it working.


Here is the code sample.

C#
async Task<List<int>> Counting()
 {
     List<int> nums = new List<int>();
     for (int i = 0; i < 100; i++)
     {
         Thread.Sleep(100);
         nums.Add(i);
     }
     return nums;

 }


 public async void AsyncAndAwait()
 {
     Console.WriteLine("AsyncAndAwait starting...");
     var task = Counting();
     Console.WriteLine("Doing something else...");


     var list = await task;
     Console.WriteLine("I am done!!!");
 }

 public void RunTest()
 {
     AsyncAndAwait();
 }
Posted
Updated 8-Nov-14 4:56am
v3
Comments
Afzaal Ahmad Zeeshan 8-Nov-14 3:26am    
Ummm, have you tried adding a Console.WriteLine("Inside the Counting method"); to your Counting method? Just to make sure it goes there and then comes back etc?
Richard MacCutchan 8-Nov-14 6:15am    
That is because the two tasks are independently scheduled by the operating system. But since they are asynchronous there is no guarantee which order they will be run in.

 
Share this answer
 
Comments
BillWoodruff 8-Nov-14 4:17am    
I am sure the OP would appreciate you describing ... in brief ... how the article linked to explains the behavior of his code, and, so would I.
[no name] 8-Nov-14 11:20am    
Link is very informative. My 5.
Following is the code which we should be writing to fulfill your conditions:

async Task<List<int>> Counting()
{
    Console.WriteLine("....something else...Started");
    List<int> nums = new List<int>();
    for (int i = 0; i < 100; i++)
    {
        // Thread.Sleep(100);
        await Task.Delay(100);
        nums.Add(i);
    }
    Console.WriteLine(".....something else...Done");
    return nums;
}
public async void AsyncAndAwait()
{
    Console.WriteLine("AsyncAndAwait starting...");
    Task<List<int>> task = Counting();
    Console.WriteLine("Doing something else...");
    List<int> list = task.Result;
    Console.WriteLine("I am done!!!");
}

public void RunTest()
{
    AsyncAndAwait();
}


I can see few reasons why your code is not giving desired behaviors:

1. Just putting async while declaring method does not make it asynchronous method. There must be await inside it. As stated at MSDN page : "If the method that the async keyword modifies doesn't contain an await expression or statement, the method executes synchronously." That's why instead of "Thread.Sleep(100)" I used "await Task.Delay(100)".

2. Just by writing "var task = Counting()", you are calling "Counting" method on same thread. if you will not use await keyword it is a blocking call. As stated at MSDN page : "An await expression does not block the thread on which it is executing." For more please visit:

http://stackoverflow.com/questions/23022573/calling-async-methods-from-a-synchronous-context[^]

Hope it will help. Thanks.
 
Share this answer
 
v3
Comments
mskataria 11-Nov-14 1:50am    
Thanks Snesh, it worked. I put Thread.Sleep to simulate delay, we beautifully changed it to "await Task.Delay" because we had awaitable delay method.

Suppose the actual task is to talk to some service over socket or to do some DB operation, where you don't have "awaitable" method, won't I be able to do that async using async keyword?
Snesh Prajapati 11-Nov-14 3:33am    
Most Welcome...A method having asynchronous behaviours must be marked with async keyword. As far as pure asynchrony is concerned in C#, we can achieve that by using delegate or events too. Such different models are described here: http://msdn.microsoft.com/en-us/library/jj152938%28v=vs.110%29.aspx Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900