Questions tagged [synchronizationcontext]

A .NET target to dispatch some work to. In winforms and WPF this is usually to the main UI thread.

A .NET target to dispatch some work to. In winforms and WPF this is usually to the main UI thread.

References

173 questions
88
votes
2 answers

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

Consider the following code of windows forms: private async void UpdateUIControlClicked(object sender, EventArgs e) { this.txtUIControl.Text = "I will be updated after await - i hope!"; await…
Yawar Murtaza
  • 3,655
  • 5
  • 34
  • 40
75
votes
5 answers

Why is TaskScheduler.Current the default TaskScheduler?

The Task Parallel Library is great and I've used it a lot in the past months. However, there's something really bothering me: the fact that TaskScheduler.Current is the default task scheduler, not TaskScheduler.Default. This is absolutely not…
37
votes
2 answers

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

I am still learning the whole Task-concept and TPL. From my current understanding, the SynchronizationContext functions (if present) are used by await to dispatch the Task "somewhere". On the other hand, the functions in the Task class do not use…
Imi
  • 1,579
  • 1
  • 12
  • 22
35
votes
3 answers

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

There are lots of guidelines for when to use ConfigureAwait(false), when using await/async in C#. It seems the general recommendation is to use ConfigureAwait(false) in library code, as it rarely depends on the synchronization context. However,…
35
votes
1 answer

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

Thanks to Jeremy Miller's good work in Functional Programming For Everyday .NET Development, I have a working command executor that does everything I want it to (do heavy lifting on the thread pool, send results or errors back to the synchronization…
flipdoubt
  • 13,897
  • 15
  • 64
  • 96
32
votes
2 answers

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

Lets looks at some simple C# async/await code where I have an object reference (obj) before and after an await with ConfigureAwait(false) private async Task AnAsyncLibraryMethod(SomeObject obj) { …
DeepSpace101
  • 13,110
  • 9
  • 77
  • 127
22
votes
2 answers

Why is SynchronizationContext.Current null in my Winforms application?

I just wrote this code: System.Threading.SynchronizationContext.Current.Post( state => DoUpdateInUIThread((Abc)state), abc); but System.Threading.SynchronizationContext.Current is null
Ian Ringrose
  • 51,220
  • 55
  • 213
  • 317
21
votes
1 answer

SynchronizationContext.Current is null in Continuation on the main UI thread

I've been trying to track down the following issue in a Winforms application: The SynchronizationContext.Current is null in a task's continuation (i.e. .ContinueWith) which is run on the main thread (I expect the the current synchronization context…
Matt Smith
  • 17,026
  • 7
  • 53
  • 103
18
votes
2 answers

Semaphore Wait vs WaitAsync in an async method

I'm trying to find out what is the difference between the SemaphoreSlim use of Wait and WaitAsync, used in this kind of context: private SemaphoreSlim semaphore = new SemaphoreSlim(1); public async Task Get() { // What's the difference…
mo5470
  • 937
  • 3
  • 10
  • 26
17
votes
2 answers

how can i force await to continue on the same thread?

await does not guarantee continuation on the same task for spawned tasks: private void TestButton_Click(object sender, RoutedEventArgs e) { Task.Run(async () => { Debug.WriteLine("running on task " + Task.CurrentId); await…
Benni
  • 1,030
  • 2
  • 11
  • 18
16
votes
4 answers

Why the default SynchronizationContext is not captured in a Console App?

I'm trying to learn more about the SynchronizationContext, so I made this simple console application: private static void Main() { var sc = new SynchronizationContext(); SynchronizationContext.SetSynchronizationContext(sc); …
13
votes
1 answer

How to get a WinForm synchronization context or schedule on a WinForm thread

I have a winform application, and an observable set up like this: Form form = new Form(); Label lb = new Label(); form.Controls.Add(lb); Observable.Interval(TimeSpan.FromSeconds(1)) .Subscribe(l => lb.Text =…
Boris
  • 5,094
  • 4
  • 45
  • 71
13
votes
1 answer

ASP.Net vs MVC vs WebAPI and UseTaskFriendlySynchronizationContext

I've got a couple ASP.Net MVC and WebAPI projects. Most of them are up to date (MVC 5 / WebAPI 2). I've been double-checking my security assumptions since I'm implementing a global filter (for MVC) and a delegating handler (for WebAPI) to unify…
12
votes
1 answer

Is ConfigureAwait(false) needed/beneficial when awaiting async calls in Azure Functions

It is generally recommended to use ConfigureAwait(false) when awaiting async calls when context is not required. Just wondering is there any benefit of using ConfigureAwait(false) in Azure Functions. Do Azure Function threads have non-null…
12
votes
2 answers

.NET: How do I invoke a delegate on a specific thread? (ISynchronizeInvoke, Dispatcher, AsyncOperation, SynchronizationContext, etc.)

Note first of all that this question is not tagged winforms or wpf or anything else GUI-specific. This is intentional, as you will see shortly. Second, sorry if this question is somewhat long. I try to pull together various bits of information…
1
2 3
11 12