synchronizationcontext

Why is TaskScheduler.Current the default TaskScheduler?

隐身守侯 提交于 2019-12-28 02:26:29
问题 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 obvious at first glance in the documentation nor samples. Current can lead to subtle bugs since its behavior is changing depending on whether you're inside another task. Which can't be determined easily. Suppose I am writting a library of asynchronous

Can't set synchronization context when using appdomains

无人久伴 提交于 2019-12-25 01:47:12
问题 I have a custom framework where a host application runs an event loop and loads a guest application into a separate app-domain. The guest application has means to take advantage of the event loop via a provided API. I want to make the guest application be able to automatically propagate all continuations onto the event loop much like it's done in .NET GUI applications and the UI thread. Therefore, I create a custom synchronization context which is able to do that. But the problem is I can't

Synchronization context for Task.Delay

徘徊边缘 提交于 2019-12-24 10:28:12
问题 I could find out that, Task.Run executes always on threads from .NET Framework threads pool ( TaskScheduler.Default ). I suppose, that it is the same with Task.Delay , but I'm not sure. MSDN says for Task.Delay only: Creates a task that will complete after a time delay Therefore the question: Where (in which synchronization context) runs Task.Delay ? 回答1: Task.Delay doesn't run anywhere . It just creates a task that completes after the specified time. Unlike Task.Run it's not accepting a

Exception practices when creating a SynchronizationContext?

柔情痞子 提交于 2019-12-24 05:58:37
问题 I'm creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I'm wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used to Send (execute synchronously) or Post (execute asynchronously) delegates of type SendOrPostCallback . Although in both cases I invoke the delegate on a STA thread, its easy to know how to handle exceptions when executing synchronously. I block the calling thread, Invoke the callback on my worker

How to get a Synchronization Context for the second form shown

自闭症网瘾萝莉.ら 提交于 2019-12-22 10:35:54
问题 [EDIT] Rephrased and Simplified whole post [/EDIT] In this blog, the following (I simplified it a bit) is given as an example of using a SynchronizationContext object to run a Task on the UI thread: Task.Factory.StartNew(() =>"Hello World").ContinueWith( task => textBox1.Text = task.Result, TaskScheduler.FromCurrentSynchronizationContext()); I can repeat these results in a fresh project, updating the UI safely, but for whatever reason in my current project (even though it's been working) I

ConfigureAwait when not awaiting

怎甘沉沦 提交于 2019-12-22 00:02:22
问题 I have an async method I am using to offload a few seconds' worth of fire-and-forget work so as not to slow down my page load. This work needs a bit of general setup and tidy-up; I want the (fast) setup to throw synchronously if it throws, but I don't want to force the tidy-up to run in the ASP context so I am using ConfigureAwait on the bit I am awaiting: public Task FireAndForget() { DoSetup(); return FireAndForgetAfterSetup(); } private async Task FireAndForgetAfterSetup() { await

Is the phrase from a book “The current SynchronizationContext is a property of the current thread” correct"?

允我心安 提交于 2019-12-21 22:18:34
问题 Having read the phrase "The current SynchronizationContext is a property of the current thread" correct", I am a little confused... In a C# app code in VS2010, when I type Thread.CurrentThread. I am not finding in the drop-down list of choices given by Intellisense any context-related properties for a thread. I know that current synchronization context can be got through " = SynchronizationContext.Current; " . But this is not quite fortunate with simultaneously executed in parallel threads,

how to forget synchronization context in async methods in c#

人盡茶涼 提交于 2019-12-21 18:35:07
问题 Let's say I want to write an async method M. I don't know what kind of synchronization context will be used (UI, ASP.NET, Console app, etc.) to call it. I'd like to make the method as easy to use as possible. That means that anyone should be able to call it synchronously by accessing the Result member of the returned Task. public async Task<int> M() { // lot's of calling of helper methods including awaits, etc. // helper methods not owned by me // ... return 42; } // this should be safe to do

await not using current SynchronizationContext

我是研究僧i 提交于 2019-12-19 09:04:53
问题 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

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

感情迁移 提交于 2019-12-18 05:06:10
问题 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