0
0
C Sharp (C#)programming~20 mins

ConfigureAwait behavior in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
ConfigureAwait Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of ConfigureAwait(false) in async method

What will be the output of this C# program?

using System;
using System.Threading.Tasks;

class Program
{
    static async Task Main()
    {
        Console.WriteLine($"Start Thread: {Environment.CurrentManagedThreadId}");
        await Task.Delay(100).ConfigureAwait(false);
        Console.WriteLine($"After await Thread: {Environment.CurrentManagedThreadId}");
    }
}
C Sharp (C#)
using System;
using System.Threading.Tasks;

class Program
{
    static async Task Main()
    {
        Console.WriteLine($"Start Thread: {Environment.CurrentManagedThreadId}");
        await Task.Delay(100).ConfigureAwait(false);
        Console.WriteLine($"After await Thread: {Environment.CurrentManagedThreadId}");
    }
}
AStart Thread: 1\nAfter await Thread: 3
BStart Thread: 3\nAfter await Thread: 3
CStart Thread: 1\nAfter await Thread: 1
DStart Thread: 3\nAfter await Thread: 1
Attempts:
2 left
💡 Hint

Think about how ConfigureAwait(false) affects thread continuation.

Predict Output
intermediate
2:00remaining
Effect of ConfigureAwait(true) on UI thread

Consider a UI application where the main thread ID is 1. What will this code print?

using System;
using System.Threading.Tasks;

class Program
{
    static async Task Main()
    {
        Console.WriteLine($"Before await Thread: {Environment.CurrentManagedThreadId}");
        await Task.Delay(50).ConfigureAwait(true);
        Console.WriteLine($"After await Thread: {Environment.CurrentManagedThreadId}");
    }
}
C Sharp (C#)
using System;
using System.Threading.Tasks;

class Program
{
    static async Task Main()
    {
        Console.WriteLine($"Before await Thread: {Environment.CurrentManagedThreadId}");
        await Task.Delay(50).ConfigureAwait(true);
        Console.WriteLine($"After await Thread: {Environment.CurrentManagedThreadId}");
    }
}
ABefore await Thread: 2\nAfter await Thread: 1
BBefore await Thread: 1\nAfter await Thread: 2
CBefore await Thread: 2\nAfter await Thread: 2
DBefore await Thread: 1\nAfter await Thread: 1
Attempts:
2 left
💡 Hint

ConfigureAwait(true) captures the synchronization context.

🔧 Debug
advanced
2:00remaining
Identify the error caused by missing ConfigureAwait(false)

What problem can occur in this code snippet when ConfigureAwait(false) is not used?

public async Task LoadDataAsync()
{
    await Task.Delay(1000); // Simulate data loading
    UpdateUI();
}

void UpdateUI()
{
    // Update UI elements here
}
C Sharp (C#)
public async Task LoadDataAsync()
{
    await Task.Delay(1000); // Simulate data loading
    UpdateUI();
}

void UpdateUI()
{
    // Update UI elements here
}
ANo error; code runs fine on UI thread.
BDeadlock due to blocking on async call.
CInvalidOperationException because UpdateUI runs on non-UI thread.
DCompile-time error due to missing ConfigureAwait.
Attempts:
2 left
💡 Hint

Think about thread affinity for UI updates.

📝 Syntax
advanced
2:00remaining
Correct usage of ConfigureAwait in async method

Which of the following code snippets correctly uses ConfigureAwait to avoid capturing the synchronization context?

Aawait SomeAsyncMethod.ConfigureAwait(false);
Bawait SomeAsyncMethod().ConfigureAwait(false);
Cawait SomeAsyncMethod().ConfigureAwait();
Dawait ConfigureAwait(false).SomeAsyncMethod();
Attempts:
2 left
💡 Hint

Remember ConfigureAwait is called on the task returned by the async method.

🚀 Application
expert
2:00remaining
Choosing ConfigureAwait usage in library code

You are writing a library method that performs asynchronous work. The library should not assume the caller's synchronization context. Which ConfigureAwait usage is best practice inside your library method?

AAlways use ConfigureAwait(false) to avoid capturing context.
BAlways use ConfigureAwait(true) to capture context for caller.
CDo not use ConfigureAwait at all and let caller decide.
DUse ConfigureAwait(false) only if caller passes a flag.
Attempts:
2 left
💡 Hint

Think about library code independence from UI or synchronization contexts.