Asychronous Programming Flashcards Preview

.NET / C# > Asychronous Programming > Flashcards

Flashcards in Asychronous Programming Deck (31)
Loading flashcards...
1

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

It hangs the application (app stopped working).

2

What is the difference between webclient and httpclient?

Webclient is legacy and HTTP client supports async methods.

3

How to use asynchronous code in a method?

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.

4

What does the await operator do?

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.

5

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

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.

6

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

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

7

When are the common scenarios to use async await?

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

8

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?

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

9

Why some methods are called MethodX and some MethodXAsync?

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

10

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

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

11

What is the so called continuation?

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

12

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

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

13

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

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

14

Should we use async VOID methods?

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.

15

What is the proper way of declaring an async method?

public async Task myMethod(){}

16

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

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

17

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

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

18

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

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

19

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

AFTER the var myReturn = await myAsyncMethod.

20

Can I use Task in any .NET framework application?

Yes. Very common even in asp.net.

21

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

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

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)?

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

23

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?

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

Can async tasks trigger other async tasks?

Yes. No caveats here.

25

Should we always validate the status of the Task?

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

26

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

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

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

Yes. Just delegate.

28

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

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

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

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

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

Is no thread-safe. Use ConcurrentBag instead.