Asychronous Programming Flashcards Preview

.NET / C# > Asychronous Programming > Flashcards

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

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

It hangs the application (app stopped working).


What is the difference between webclient and httpclient?

Webclient is legacy and HTTP client supports async methods.


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.


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.


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.


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.


When are the common scenarios to use async await?

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


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.


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.


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.


What is the so called continuation?

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


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)


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.


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.


What is the proper way of declaring an async method?

public async Task myMethod(){}


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.


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.


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.


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

AFTER the var myReturn = await myAsyncMethod.


Can I use Task in any .NET framework application?

Yes. Very common even in


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)


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(() => {})
You need to change the calling method signature to async.


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.


Can async tasks trigger other async tasks?

Yes. No caveats here.


Should we always validate the status of the Task?

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


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.

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


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

Yes. Just delegate.


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.


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.


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

Is no thread-safe. Use ConcurrentBag instead.