FlashKeepers

C# · College · College and University

C# IV: Async, Await and Threading

Task, async and await, ConfigureAwait, cancellation tokens, Parallel, locks and thread safety in C#.

About this set

40 basic cards for College C#. It opens with "What is an async method in C#?" and "What does the await keyword do?". Study it alongside the other College sets below.

40 cards · basic cards · AI-written, checked twice. Edit anything.

Study this set free Look inside first Get FlashKeepers for iPhone

Opens in your browser, no account. Get the iPhone and iPad app · Android app

What is an async method in C#?
Show answerA method that can pause execution and resume later without blocking threads, marked with the async keyword.
What does the await keyword do?
Show answerPauses execution of an async method until the awaited Task completes, then resumes automatically.
What is the difference between Task and Task<T>?
Show answerTask represents an operation with no return value. Task<T> represents an operation that returns a value of type T.
What does ConfigureAwait(false) do?
Show answerPrevents the continuation from running on the original synchronization context after the await.
When should you use ConfigureAwait(false)?
Show answerIn library code where you do not need to return to the UI or original synchronization context.
What is a cancellation token in C#?
Show answerA mechanism to signal that an operation should stop, preventing unwanted work from running.
What class provides cancellation tokens?
Show answerCancellationTokenSource creates tokens. CancellationToken is the parameter type passed to operations.
How do you check if cancellation was requested?
Show answerCall ThrowIfCancellationRequested() to throw an exception, or check the IsCancellationRequested property.
What does Parallel.For do?
Show answerExecutes loop iterations concurrently across multiple threads instead of sequentially.
What is the difference between Parallel.For and a regular for loop?
Show answerParallel.For may execute iterations out of order and concurrently. A regular for loop is strictly sequential.
What is PLINQ?
Show answerParallel LINQ. Adding .AsParallel() to a LINQ query makes it execute across multiple cores.
What is the lock keyword used for?
Show answerAcquires an exclusive lock on an object to ensure only one thread accesses code at a time.
What happens if a thread tries to acquire a lock that is already held?
Show answerThe thread blocks until the lock is released by the thread holding it.
What is Monitor.Enter and Monitor.Exit?
Show answerLow-level synchronization methods: Monitor.Enter acquires a lock on an object, Monitor.Exit releases it.
What is a Mutex in C#?
Show answerA kernel-level synchronization primitive that ensures only one thread can access a resource at a time. Can be named and cross-process.

25 more cards in the app

Study this set free