# Luna Tech

Tutorials For Dummies.

# 0. What is Async Programming?

Async Programming means our task will be running on a different thread and once it is completed, the original thread will continue with the information gathered from the Async task.

## Why do we need Async Programming?

• Usually we don’t want to block UI while doing some time consuming tasks
• Sometimes we want to make sure the server can do some other tasks while waiting for the I/O operation to be completed
• In short, increase user experience as well as make our application more efficient.

## Difference between Async Programming and Parallel Programming

Async Programming has continuaty, the current thread will subscribe to the Async Task and continue the execution from there.

Parallel Programming are often used to utilize CPU by dividing a problem into smaller pieces that are solved independently.

Basically, they are solving different problems.

But in C#, they can both use Task Parallel Library, so there might be some confusion around it.

# 1. Implementation

## Async/Await keywords

They usually go hand in hand, if you have an async method, the caller must await for it.

Because we cannot catch exceptions and deal with them.

## Method signature

We don’t need to add Async in the method name.

## Chaining

We can use ContinueWith to chain tasks.

## Reflection of the async/await method chain

The underlining implementation of Async/Await is to create a state machine for the method, bear this in mind and be cautious of chaining multiple async/await calls, it might introduce performance issue.

Note: Even though async and await introduces overhead, it ensures that the code is awaited and makes it potentially easier to change, as you can expand with code in the continuation.

If there’s nothing required in continuation, then a better solution is to keep the method signature without async/await, and only the first method is marked as async (only top level uses async/await).

e.g., this is an example of multiple state machines caused by a method chain.

async Task Main()
{
await Run();
}

return await Compute();
}

}

}


It can be simplified as the following:

async Task Main()
{
await Run();
}

return Compute();
}

}

}


# 2. Common Considerations

1. How to get the result and use it? (use await and then use the result)
2. How to handle exceptions? (try catch blocks)
3. How to handle success and failure tasks? (check operator)
4. How to resume the UI thread? (use Dispatcher.Invoke)
6. If we have multiple async calls running in parallel, how do we know one or all of them have been completed? (put await in a proper place and use WhenAll/WhenAny to check)
7. How to test async methods? (use interface/strategy pattern and add a mock class to implement the interface)
8. How to process parallel tasks once each call finishes (use ContinueWith to access the result)
9. How to create a thread safe list to hold the parallel tasks’ result (use ConcurrentBage)
10. What to do if you don’t care about the original context (set ConfigureAwait to false)
11. How to use stream async to improve user experience? (AsyncEnumerable, yield, observableCollection, file stream reaader, AsyncDisposable)

# 3. Common Issues

1. deadlock: caused by calling result or wait before await.
2. mess up with the threads: if we have 3 tasks, A is cancelled, B is continue with A, C is continued with A, then both B and C knows A’s status; but if we chain C with B, C will only check B’s status, not A’s.

# 4. Tips

You can turn on the Visual Studio debugger thread window to check the threads easily.