What does ‘context’ exactly mean in C# async/await code?

If I’m not totally wrong, ConfigureAwait(false); only means that the code which runs after the code you are awaiting, is not required to use the SynchronizationContext from before the await. SynchronizationContext can be different things, as Stephen pointed out. Imagine you are in a web environment and your code after the await relies on HttpContext.Current.Items: … Read more

What is the difference between SynchronizationContext.Send and SynchronizationContext.Post?

Send – synchronous: wait for answer (or action completed) Post – asynchronous: drop off and continue So your example uses the correct methods at the right moments. There is no need to halt the for-loop until the progress update is complete (on the contrary). And Execute does want to wait for the Action to complete, … Read more

Should we use ConfigureAwait(false) in libraries that call async callbacks?

When you say await task.ConfigureAwait(false) you transition to the thread-pool causing mapping to run under a null context as opposed to running under the previous context. That can cause different behavior. So if the caller wrote: await Map(0, i => { myTextBox.Text = i.ToString(); return 0; }); //contrived… Then this would crash under the following … Read more

How to get a Task that uses SynchronizationContext? And how are SynchronizationContext used anyway?

How can I obtain a Task, that actually runs an action but is dispatched using SynchronizationContext.Current.Send/Post? Use special task scheduler: Task.Factory.StartNew( () => {}, // this will use current synchronization context CancellationToken.None, TaskCreationOptions.None, TaskScheduler.FromCurrentSynchronizationContext()); And can anyone recommend a good introduction into SynchronizationContext Look at the article It’s All About the SynchronizationContext by Stephen Cleary.

Why is TaskScheduler.Current the default TaskScheduler?

I think the current behavior makes sense. If I create my own task scheduler, and start some task that starts other tasks, I probably want all the tasks to use the scheduler I created. I agree that it’s odd that sometimes starting a task from the UI thread uses the default scheduler and sometimes not. … Read more

Why would I bother to use Task.ConfigureAwait(continueOnCapturedContext: false);

Stephen Cleary has a really good series on this you can find here, I quoted the piece specific to your question: Most of the time, you don’t need to sync back to the “main” context. Most async methods will be designed with composition in mind: they await other operations, and each one represents an asynchronous … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)