2

Lets say I have this code:

public async void Init(){
    init1();
    init2();
    init3();     
}

First question is are they running asynchronously or not? The methods themselves are public void not async.

Second question is how do I make sure they're running async and to know when they're finished? Like this..

public async void Init(){
    init1();
    init2();
    init3();     
    await ... all finished
}

Third, when I call Init() elsewhere, can it not be wrapped by an async method? Like this code below, or does it have to be async?

public async void Init(){
    init1();
    init2();
    init3();   
}

public void doIt(){
  Init();
}
4
  • Side note: void Foo can't be await'ed, so really not much difference difference in usage whether it is async or not... Trying to reason about behavior of such method should cause pain... Commented Oct 3, 2014 at 1:11
  • This one page will answer pretty much all your questions. msdn.microsoft.com/en-nz/library/hh191443.aspx Commented Oct 3, 2014 at 1:13
  • @sa_ddam213 I did look at that already, and even though it helps with using httpclient, and methods that return data, but not what I'm asking. Commented Oct 3, 2014 at 1:14
  • Well, 1. your init methods are not Tasks or awaited so they will run synchronously. 2. Make them all Tasks and use WaitAll, 3. yes, NOTE: you should not be using async with voids (unless its an event handler) you should be using the TPL logic Commented Oct 3, 2014 at 1:19

2 Answers 2

9

I have an async intro that should help.

First question is are they running asynchronously or not?

No, and you didn't really need to ask this on Stack Overflow because the compiler itself will give you a warning that explicitly states your method will run synchronously.

Second question is how do I make sure they're running async and to know when they're finished?

First, your methods need to be properly asynchronous:

async Task init1Async();
async Task init2Async();
async Task init3Async();

Then you can make the caller invoke them concurrently and then asynchronously wait for them all to complete:

async Task InitAsync() {
  var task1 = init1Async();
  var task2 = init2Async();
  var task3 = init3Async();
  await Task.WhenAll(task1, task2, task3);
}

Third, when I call Init() elsewhere, can it not be wrapped by an async method?

Once your InitAsync method is properly defined as returning a Task, it can be naturally consumed using await:

async Task doItAsync() {
  await InitAsync();
}

Note that this does require the caller to be async.

Also recommended reading: my Best Practices for Asynchronous Code MSDN article, particularly the sections on "avoid async void" and "async all the way".

Sign up to request clarification or add additional context in comments.

3 Comments

Lol I actually stumbled across your page before this and checked out the examples. Your answer here pretty much sums it up. However, if I don't make each method async init1Async() can I just use Task.Run in the InitAsync()? If so, would InitAsync() also have to be async to use Task.Run?
+1. @ZeeTee No if you don't await call to Task.Run you should not mark method async (as Stephen Cleary pointed out if you'd be looking at compiler warning you'd see warning about it too).
@ZeeTee: In general, you should avoid Task.Run on ASP.NET. If you have naturally synchronous (e.g., CPU-bound) work to do, just call it directly without async/await or Task.Run. If you have naturally asynchronous (e.g., I/O-bound) work to do, call the asynchronous API using async/await, but without using Task.Run. P.S. I have an MSDN article on async ASP.NET that just came out a couple days ago.
0

Also One Thing you can do await on each on the methods to get the required result of each methods.

var task1 = await init1Async();
var task2 = await  init2Async();
var task3 = await init3Async();

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.