synchronizationcontext

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

南笙酒味 提交于 2019-11-27 02:10:26
问题 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 the context, right? So for example Task.Run(...) will always dispatch the action on an worker thread of the thread pool and ignore the SynchronizationContext.Current completely. await Foobar() would use the context to execute the generated task after

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

僤鯓⒐⒋嵵緔 提交于 2019-11-26 21:39:09
问题 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); DoSomething().Wait(); } private static async Task DoSomething() { Console.WriteLine(SynchronizationContext.Current != null); // true await Task.Delay(3000); Console.WriteLine(SynchronizationContext.Current != null); // false! why ? } If I understand correctly, the await

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

拟墨画扇 提交于 2019-11-26 11:09:54
问题 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 to be System.Windows.Forms.WindowsFormsSynchronizationContext ). Here\'s the Winforms code demonstrating the issue: using System; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace

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

守給你的承諾、 提交于 2019-11-26 10:17:54
问题 Consider the following code of windows forms: private async void UpdateUIControlClicked(object sender, EventArgs e) { this.txtUIControl.Text = \"I will be updated after 2nd await - i hope!\"; await Task.Delay(5000).ConfigureAwait(continueOnCapturedContext: false); this.txtUIControl.Text = \"I am updated now.\"; } Here the exception is thrown at the 3rd line because after await the code is executed on non-UI thread. Where ConfigureAwait(false) is useful? 回答1: Stephen Cleary has a really good

Using SynchronizationContext for sending events back to the UI for WinForms or WPF

自闭症网瘾萝莉.ら 提交于 2019-11-26 07:42:37
问题 I\'m using a SynchronizationContext to marshal events back to the UI thread from my DLL that does a lot of multi-threaded background tasks. I know the singleton pattern isn\'t a favorite, but I\'m using it for now to store a reference of the UI\'s SynchronizationContext when you create foo\'s parent object. public class Foo { public event EventHandler FooDoDoneEvent; public void DoFoo() { //stuff OnFooDoDone(); } private void OnFooDoDone() { if (FooDoDoneEvent != null) { if (TheUISync