Asychronous Programming Flashcards

1
Q

What happens when you run long-processing routines in the main thread?

A

It hangs the application (app stopped working).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the difference between webclient and httpclient?

A

Webclient is legacy and HTTP client supports async methods.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How to use asynchronous code in a method?

A

The method needs to be declared as async and then within this method you can now call the await operator which will run the operations asynchronously.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does the await operator do?

A

It runs the method after the keyword in an asynchronous method. It will wait (without blocking main thread) until the call returns before continuing to run the code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the different types of asynchronous programming in C#?

A

The traditional which means manipulating threads and background workers (event-based asynch pattern). And the current way, which is via task parallel library via async away keywords.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Can we leverage the async await mechanism by simple adding an async operator in the slow method?

A

No… it’ll have no effect. Even the compiler will hint you to add at least one await within the method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

When are the common scenarios to use async await?

A

For I/O operations, such as Disk, Memory, Web/API and Databases.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What happens when you call the Result (or wait()) method of the returned task created by call of the async method? How to fix it?

A

It will execute synchronously and the UI will no longer respond until finished - sometimes causes DEADLOCK. Fixed by calling await instead.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Why some methods are called MethodX and some MethodXAsync?

A

In the past it was very common to both be used, but nowadays pretty much most of the heavy stuff be ran asynchronously.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the gain of using async await in a rest api? Aren’t all web api calls async from the caller perspective?

A

The gain is the the webserver will have a performance boost once the web server is freed to do something else.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the so called continuation?

A

Is the code that needs to be called after the await method finishes (has returned value).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Where the continuation is called? Will it restore the previous thread context?

A

The continuation is called in the original context (often UI thread). Yes, it restores the context before creating this artificial asynchronous operation (await)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Does the response of an await method allow for checking for success status codes?

A

Yes… just call EnsureSuccessStatusCode(); when the status code is not correct, it raises an exception. remember to handle it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Should we use async VOID methods?

A

No. Avoid at any cost. Only allowed for event handlers and delegates (e.g, button click)!!!! Also, make sure all the exceptions are try catched inside this async method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the proper way of declaring an async method?

A

public async Task myMethod(){}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Why don’t I get an error when declaring an public async method even without returning anything?

A

Because methods marked as async task will automatically have a Task returning without explicitly having to return anything.

17
Q

What happens when an exception happens in an async method that you called without await?

A

The exception will be swallowed by the Task and the caller will never know that it happened.

18
Q

What happens when an async method that returns void instead of a Task is called and an exception occurs inside this method?

A

The application terminates because the exception is not handled by anyone, therefore leaks to the application itself and it terminates the app.

19
Q

If I can’t call the result method directly, how can I access the result then?

A

AFTER the var myReturn = await myAsyncMethod.

20
Q

Can I use Task in any .NET framework application?

A

Yes. Very common even in asp.net.

21
Q

How to create an asynchronous code block? Can I interact with objects that are not instantiated by my thread?

A

By simply calling Task.Run(() => {code here}). You cannot interact with not-owned objects. You need to execute this code in the main thread context with Dispatcher.Invoke(() => main thread manipulation code here)

22
Q

How to make sure an async code block is finished before calling the rest of the code (that is likely to depend on the async process)?

A

Simply call await Task.Run(() => {})
Note:
You need to change the calling method signature to async.

23
Q

Can the continuation be customized when you call Task.Run? What happens if the async task itself fails? How to configure it? Is the continuation also running in a different thread?

A

Yes. But is a bit chatty. When the async task fails it will execute the continuation by default. To configure it, use TaskContinuationOptions enum. Task continuation also executes on a different thread/context.

24
Q

Can async tasks trigger other async tasks?

A

Yes. No caveats here.

25
Q

Should we always validate the status of the Task?

A

Yes. Necessary to make sure the program will behave consistently even when the task fails.

26
Q

How to stop an async task in the middle of the process? Should my task code be aware of the this token?

A

By creating a CancellationTokenSource and sending to the async task. The UI thread can call CancellationTokenSource.Cancel to cancel the task.
Yes, you need to check from time to time if the token has IsCanellationRequested equals true and then return as soon as possible.

Note:
Whenever you create an async task you should accept cancellation tokens so that the caller can stop earlier the process.

27
Q

Can I subscribe to the CancellationTokenSource.Token so that I can react when the cancellation happens?

A

Yes. Just delegate.

28
Q

How to wait if a list of tasks (IEnumerable) are all completed? Is it async?

A

Task.WhenAll(myEnum). Yes, it is async. You can call await Task.WhenAll(myEnum).

Note: it is recommended to define a timeout for the tasks.

29
Q

What to do when you want to mock an async method for testing purposes? Do you really need to call it asynchronously?

A

No need to call it async. Just call return Task.FromResult(expected class here) and it will run synchronously without affecting the code that is expecting a task.

30
Q

Is List thread safe? If not, which class fulfills this purpose?

A

Is no thread-safe. Use ConcurrentBag instead.

31
Q

How to make the continuation to also execute in the task thread? Is it dangerous? What is the recommendation for library developers? Does it improve the performance?

A

by configuring the task call with: await mymethod().ConfigureAwait(false). Yes, DANGEROUS, use with care. Library developers should use it as much as possible once they are likely to don’t care about other threads’ context. Yes, improve the performance once there’s no need to switch the thread context.
Note: Only affects current method.