2

This is the program output:

Program Begin
1 - Starting
2 - Task started
A - Started something
Program End
B - Completed something
3 - Task completed with result: 123

Question: As far i understand when it comes to await process is going back to main context so in this case to Main and then go back to await when it's finished so "A - Started something" should be after "Program End". Why this one line was shown? From my understanding when it comes to away it should immediatly go back to main context.

static void Main(string[] args)
        {
            Console.WriteLine("Program Begin");
            DoAsAsync();
            Console.WriteLine("Program End");
            Console.ReadLine();
        }

        static async void DoAsAsync()
        {
            Console.WriteLine("1 - Starting");
            var t = Task.Factory.StartNew<int>(DoSomethingThatTakesTime);
            Console.WriteLine("2 - Task started");
            var result = await t;
            Console.WriteLine("3 - Task completed with result: " + result);
        }

        static int DoSomethingThatTakesTime()
        {
            Console.WriteLine("A - Started something");
            Thread.Sleep(1000);
            Console.WriteLine("B - Completed something");
            return 123;
        }
2
  • This is why you should never use async void.
    – SLaks
    Oct 20, 2016 at 1:28
  • That's just coincidence that the other thread ran that then.
    – SLaks
    Oct 20, 2016 at 1:30

2 Answers 2

0

As far i understand when it comes to await process is going back to main context so in this case to Main and then go back to await when it's finished so "A - Started something" should be after "Program End".

That is what that thread is doing; the main thread is returning to the Main method.

However, StartNew will (in this case) execute its work on a thread pool thread, which runs independently from the main thread. So, you might see "A - Started something" before or after "Program End".

7
  • i thought task is not thread itself but could work on main thread asynchronously
    – user6156963
    Oct 20, 2016 at 22:48
  • 1
    @JimmyJimm: Yes, but by using StartNew, you're explicitly saying "run this task on a thread pool thread, not the UI thread". Oct 21, 2016 at 1:29
  • so what you saying is when i type Task.Factory.StartNew - this means run on thread pool and when i would choose Task.Run?
    – user6156963
    Oct 21, 2016 at 11:44
  • 1
    @JimmyJimm: To be clear, you should use Task.Run to run code on a thread pool thread. Do not use StartNew at all - it's a low-level API that is very difficult to use correctly. Oct 21, 2016 at 14:59
  • I decided to not use StartNew anymore but just Task.Run so i convert to it all i got with StartNew. But Task.Run is just shortcut for StartNew(with specific parameters..) so is it not the same?
    – user6156963
    Oct 21, 2016 at 18:58
0

When it comes to await, first the task get triggered, here it is "DoSomethingThatTakesTime". And then goes back to calling method to perform the next task/execution (here in your case it is Main()).

This will be much meaning full if you have another await in the main.

look into below example. very well explained. https://msdn.microsoft.com/en-us/library/mt674892.aspx

You may see a different result, if you call thread sleep before first print statement.

14
  • exactly so still don't get it... task get triggered but should not go inside at this moment. Should go inside after "Program End"
    – user6156963
    Oct 20, 2016 at 1:50
  • That's not mandatory. Both are two different execution paths once "DoSomethingThatTakesTime" is triggered (two tasks running in parallel). Oct 20, 2016 at 1:59
  • I would say main thread and task which works on that thread in behind.. When code reaches await it's calling back immediatly to the main context and then goes back again to await method therefore i do not understand both sequences.
    – user6156963
    Oct 20, 2016 at 2:01
  • "it is not goes back again". based on print statement you may feel like it goes back again. one execution comes back to main method and prints statements in main. The other execution goes to "DoSomethingThatTakesTime" prints statements there. Both are isolated Oct 20, 2016 at 2:05
  • so i lost understanding of away keyword - i thought when it reaches it calls back to main context immediatly (not going to the DoSomethingThatTakesTime) at this point but after went back from Main. If not then why it's not continue executing away method but executing one statment of that method and goes back.
    – user6156963
    Oct 20, 2016 at 2:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.