synchronizationcontext

Get SynchronizationContext from a given Thread

Deadly 提交于 2019-11-29 04:27:40
I seem not to find how to get the SynchronizationContext of a given Thread : Thread uiThread = UIConfiguration.UIThread; SynchronizationContext context = uiThread.Huh?; Why should I need that? Because I need to post to the UIThread from different spots all across the front end application. So I defined a static property in a class called UIConfiguration . I set this property in the Program.Main method: UIConfiguration.UIThread = Thread.CurrentThread; In that very moment I can be sure I have the right thread, however I cannot set a static property like UIConfiguration.SynchronizationContext =

Workaround for issue in .NET 4.0 where SynchronizationContext.Current is null

你。 提交于 2019-11-28 10:29:20
What is a workaround for the issue where the SynchronizationContext.Current is null unexpectedly on the main thread in .NET 4.0? See: SynchronizationContext.Current is null in Continuation on the main UI thread Matt Smith I created several extension methods that matched ContinueWith and StartNew except that they also take an additional SyncronizationContext . I then use this argument to restore the expected SynchronizationContext before executing the action: Below, I've given examples: public static class TaskExtensionMethods { public static Task ContinueWith_UsingSyncContextWorkaround(this

Why a unique synchronization context for each Dispatcher.BeginInvoke callback?

浪子不回头ぞ 提交于 2019-11-28 10:01:59
I've just noticed that with .NET 4.5 each Dispatcher.BeginInvoke / InvokeAsync callback is executed on its own very unique Synchronization Context (an instance of DispatcherSynchronizationContext ). What's the reason behind this change? The following trivial WPF app illustrates this: using System; using System.Diagnostics; using System.Threading; using System.Windows; using System.Windows.Threading; namespace WpfApplication { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); Action test = null; var i = 0; test = () => { var sc = SynchronizationContext

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

匆匆过客 提交于 2019-11-28 08:10:27
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 the await ? If that is true, my question is: How can I obtain a Task , that actually runs an action

Why SynchronizationContext does not work properly?

孤街醉人 提交于 2019-11-28 05:43:53
问题 I have following code: [TestMethod] public void StartWorkInFirstThread() { if (SynchronizationContext.Current == null) SynchronizationContext.SetSynchronizationContext( new SynchronizationContext()); var syncContext = SynchronizationContext.Current; Console.WriteLine("Start work in the first thread ({0})", Thread.CurrentThread.ManagedThreadId); var action = ((Action) DoSomethingInSecondThread); action.BeginInvoke(CallbackInSecondThread, syncContext); // Continue its own work } private static

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

微笑、不失礼 提交于 2019-11-27 20:18:40
问题 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 context, and even post progress back to the synchronization context), but I can't explain why it uses SynchronizationContext.Send from the thread-pool and Synchronization.Post from the Func passed into the method that does the heavy lifting. I

Get SynchronizationContext from a given Thread

做~自己de王妃 提交于 2019-11-27 18:25:46
问题 I seem not to find how to get the SynchronizationContext of a given Thread : Thread uiThread = UIConfiguration.UIThread; SynchronizationContext context = uiThread.Huh?; Why should I need that? Because I need to post to the UIThread from different spots all across the front end application. So I defined a static property in a class called UIConfiguration . I set this property in the Program.Main method: UIConfiguration.UIThread = Thread.CurrentThread; In that very moment I can be sure I have

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

我的未来我决定 提交于 2019-11-27 10:47:24
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? Victor Learned 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,

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

白昼怎懂夜的黑 提交于 2019-11-27 04:24:04
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 WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); TaskScheduler ts =

Why a unique synchronization context for each Dispatcher.BeginInvoke callback?

半世苍凉 提交于 2019-11-27 03:22:35
问题 I've just noticed that with .NET 4.5 each Dispatcher.BeginInvoke / InvokeAsync callback is executed on its own very unique Synchronization Context (an instance of DispatcherSynchronizationContext ). What's the reason behind this change? The following trivial WPF app illustrates this: using System; using System.Diagnostics; using System.Threading; using System.Windows; using System.Windows.Threading; namespace WpfApplication { public partial class MainWindow : Window { public MainWindow() {