ExpertGenie
ExpertGenie

Reputation: 195

Does a method has to be async when invoked inside an async method?

Lets say I have a method defined as follows:

public async Task CreateUser()
{
   await GetUserDetails();
   GetUserOrder();
}

private void GetUserDetails() {

private void GetUserOrder() {

Does the method GetUserDetails(); and GetUserOrder() have to be async as well to avoid UI blocking ?

I cannot await the GetUserDetails() method since it is not async. How can I achieve this in c# ? I want to ensure all these methods are invoked step by step.

Upvotes: 1

Views: 303

Answers (3)

Miro J.
Miro J.

Reputation: 4954

You should put await only in front of async methods. To run a synchronous one that you don't want to wait, you can use a new thread from the tread pool:

new Thread(() => DoSomething()).Start();

or

Task.Factory.SartNew(()=> DoSomething());

Here is the help page: https://msdn.microsoft.com/en-us/library/dd321439(v=vs.110).aspx

Otherwise, your call to GetUserDetails will have to finish before you execute the next line.

Upvotes: -1

Pranay Rana
Pranay Rana

Reputation: 176896

yes if you want to wait than you have to write await for that methods also. because after first await your code agian will be synchronous ..and if UI thread than it will run on it.

1.you code will be , so by this code you code become asynchronous for GetUserORder also. you just have to wrap method in Task construct and return

public async Task CreateUser()
{
   await GetUserDetails();
   await Task.Factory.SartNew(()=> GetUserOrder());
}

2.or you can do this also

public async Task CreateUser()
{
   await  Task.Factory.SartNew(()=>{
           GetUserDetails();
           GetUserOrder(); });
}

3.or you can do like this also, in below code will not wait for getuserorder method and excute await one method

 public async Task CreateUser()
    {
       Task.Factory.SartNew(()=> GetUserOrder()).ContinueWith((t)=> Console.WriteLine("Completed");
       await GetUserDetails();
    }

4.or last one variation, here you start GetUserOrder first and dont wait for it than you call GetUserDetails in async fashion , and if you want to work on GetUserOrder method want to wait just use Wait method.

 public async Task CreateUser()
    {
      var task =  Task.Factory.SartNew(()=> GetUserOrder());
       await GetUserDetails();   
       if(!task.IsCompleted)
       task.Wait(); 
    }

in your case you can go for 3 and if you want to wait go for 4th one.


As you asked me in comment what is difference between Task.Run and statnew method -: for that you can check this SO question : Regarding usage of Task.Start() , Task.Run() and Task.Factory.StartNew()

Upvotes: 1

Eric Lippert
Eric Lippert

Reputation: 659994

The relevant question is in a comment:

How can I ensure all my methods are invoked completely sequentially?

The fact that you're asking the question indicates that you don't understand what "await" is. Await is the sequencing operation on a asynchronous workflows. An await means this workflow will not proceed until the awaited task is complete. It's an asynchronous wait, hence the name await.

Consider this question: in a synchronous workflow, what is the sequencing operation?

No, really, give it some thought.

.

.

.

It is ;. When you say

fResult = foo();
bResult = bar();
qResult = qux();

that means that foo has to finish completely before bar can begin. That is not true for asynchronous workflows. If we have

fTask = fooAsync();
bTask = barAsync();
qTask = quxAsync();

Then the asynchronous operations can complete in any order. If we say

await fooAsync();
await barAsync();
await quxAsync();

Then barAsync will not start until fooAsync's task completes. await sequences the asynchronous workflow. The difference is that the thread can continue to do other unrelated work while asynchronously waiting for foo to complete, which is not true in a synchronous workflow; in a synchronous workflow the thread is already busy computing the foo result, so it can't do other work.

Upvotes: 7

Related Questions