synchronizationcontext

When does Task.Run flow SynchronizationContext with ExecutionContext?

流过昼夜 提交于 2019-12-02 00:29:39
This article from states that SynchronizationContext may flow with ExecutionContext : private void button1_Click(object sender, EventArgs e) { button1.Text = await Task.Run(async delegate { string data = await DownloadAsync(); return Compute(data); }); } Here’s what my mental model tells me will happen with this code. A user clicks button1, causing the UI framework to invoke button1_Click on the UI thread. The code then kicks off a work item to run on the ThreadPool (via Task.Run). That work item starts some download work and asynchronously waits for it to complete. A subsequent work item on

How to explain await/async Synchronization Context switching behavior

馋奶兔 提交于 2019-12-01 18:34:29
There are a couple of things (but 1 main thing) that I don't understand about the behavior of the following code. Can someone help explain this? It's actually pretty simple code - just one regular method calling an async method. And in the async method I use a using block to try to temporarily change the SynchronizationContext. At different points in the code, I probe for the current SynchronizationContext. Here are my questions: When execution reaches position "2.1" the context has changed to Context #2. Okay. Then, because we hit an `await`, a Task is returned and execution jumps back to

How to explain await/async Synchronization Context switching behavior

时间秒杀一切 提交于 2019-12-01 18:19:59
问题 There are a couple of things (but 1 main thing) that I don't understand about the behavior of the following code. Can someone help explain this? It's actually pretty simple code - just one regular method calling an async method. And in the async method I use a using block to try to temporarily change the SynchronizationContext. At different points in the code, I probe for the current SynchronizationContext. Here are my questions: When execution reaches position "2.1" the context has changed

await not using current SynchronizationContext

巧了我就是萌 提交于 2019-12-01 06:32:20
I'm getting confusing behavior when using a different SynchronizationContext inside an async function than outside. Most of my program's code uses a custom SynchronizationContext that simply queues up the SendOrPostCallbacks and calls them at a specific known point in my main thread. I set this custom SynchronizationContext at the beginning of time and everything works fine when I only use this one. The problem I'm running into is that I have functions that I want their await continuations to run in the thread pool. void BeginningOfTime() { // MyCustomContext queues each endOrPostCallback and

async/await with ConfigureAwait's continueOnCapturedContext parameter and SynchronizationContext for asynchronous continuations

不打扰是莪最后的温柔 提交于 2019-11-30 12:23:38
I would like put the code first and then explain the situation and ask my question based on that: public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private async void Button_Click_2(object sender, RoutedEventArgs e) { var result = await GetValuesAsync(); Foo.Text += result; } public async Task<string> GetValuesAsync() { using (var httpClient = new HttpClient()) { var response = await httpClient .GetAsync("http://www.google.com") .ConfigureAwait(continueOnCapturedContext: false); // This is the continuation for the httpClient.GetAsync method. // We

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

折月煮酒 提交于 2019-11-30 07:09:11
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 = l.ToString()); Application.Run(form); This doesn't work, since the l => lb.Text = l.ToString() will not be run on the main thread which created the form, but I cannot figure out how to make it run on this thread. I assume, that I should use IObservable.SubscribeOn which takes either an IScheduler or a SynchronizationContext , but I don't know how to get the synchronizationcontext of the

async/await with ConfigureAwait's continueOnCapturedContext parameter and SynchronizationContext for asynchronous continuations

吃可爱长大的小学妹 提交于 2019-11-29 18:04:24
问题 I would like put the code first and then explain the situation and ask my question based on that: public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private async void Button_Click_2(object sender, RoutedEventArgs e) { var result = await GetValuesAsync(); Foo.Text += result; } public async Task<string> GetValuesAsync() { using (var httpClient = new HttpClient()) { var response = await httpClient .GetAsync("http://www.google.com") .ConfigureAwait

Why SynchronizationContext does not work properly?

淺唱寂寞╮ 提交于 2019-11-29 11:55:20
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 void DoSomethingInSecondThread() { Console.WriteLine("Do something in the second thread ({0})", Thread

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

馋奶兔 提交于 2019-11-29 08:18:00
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 Task.Delay(TimeSpan.FromMilliseconds(100)); Debug.WriteLine("running on task " + Task.CurrentId); }); } The output of this is: running on task 1 running on task so we can see that not only the execution has moved to another task, but also to the UI-thread. How can i create a dedicated task, and enforce await to always continue on this task? Long-running tasks don't do this

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

一个人想着一个人 提交于 2019-11-29 07:23:21
问题 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 = l.ToString()); Application.Run(form); This doesn't work, since the l => lb.Text = l.ToString() will not be run on the main thread which created the form, but I cannot figure out how to make it run on this thread. I assume, that I should use IObservable.SubscribeOn which takes either an