synchronizationcontext

Comparing SynchronizationContext

爷,独闯天下 提交于 2019-12-05 02:54:10
How do I compare SynchronizationContext? It seems that the same Dispatcher can create different SynchronizationContext when using BeginInvoke. When I drill down into the two (unequal) contexts, I see that the dispatcher Thread ID is the same, yet they are not Equal to each other. public partial class MainWindow : Window { private SynchronizationContext contexta; private SynchronizationContext contextb; private SynchronizationContext contextc; private SynchronizationContext contextd; public MainWindow() { InitializeComponent(); contexta = SynchronizationContext.Current; Loaded += MainWindow

ConfigureAwait when not awaiting

醉酒当歌 提交于 2019-12-04 18:13:42
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 AFewSecondsWorthOfWork().ConfigureAwait(false); DoTidyUp(); } protected void btn_Click(object sender, EventArgs

In the context of ASP.NET, why doesn't Task.Run(…).Result deadlock when calling an async method?

删除回忆录丶 提交于 2019-12-04 12:31:31
问题 I created a simple WebApi project with a single controller and a single method: public static class DoIt { public static async Task<string> GetStrAsync(Uri uri) { using (var client = new HttpClient()) { var str = await client.GetStringAsync(uri); return str; } } } public class TaskRunResultController : ApiController { public string Get() { var task = Task.Run(() => DoIt.GetStrAsync(new Uri("http://google.com")) ); var result = task.Result; return result; } } I have a good understanding of

Security, Thread.CurrentPrincipal, and ConfigureAwait(false)

点点圈 提交于 2019-12-04 11:20:43
问题 Would using Thread.CurrentPrincipal's claims in a referenced library that uses ConfigureAwait(false) pose any problems or will the flowing of ExecutionContext's logical call context take care of me there? (my reading and testing so far indicates that it will). Example WebAPI Controller Action: [CustomAuthorizeThatSetsCurrentUsersClaimsToThreadCurrentContextAndHttpContextCurrentUser] public async Task<Order> Get(int orderId) { return await _orderBusinessLogicLibrary.LoadAsync(orderId); //

how to forget synchronization context in async methods in c#

我的梦境 提交于 2019-12-04 10:08:21
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 from any synchronization context int result = M().Result; // Synchronously wait The problem is that

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

折月煮酒 提交于 2019-12-03 10:46:56
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<SomeObject> AnAsyncLibraryMethod(SomeObject obj) { Console.WriteLine(Thread.CurrentThread.ManagedThreadId); obj.Name = "Harry"; // <-- obj here // MAIN POINT var newSubObj = await FetchOverHttpAsync().ConfigureAwait(false); // Continuation here Console.WriteLine(Thread.CurrentThread.ManagedThreadId); obj.Name = "Sally"; // <-- same obj here return obj; } public class SomeObject { public string Name; } ConfigureAwait(false) seems to means

Security, Thread.CurrentPrincipal, and ConfigureAwait(false)

只谈情不闲聊 提交于 2019-12-03 08:02:26
Would using Thread.CurrentPrincipal's claims in a referenced library that uses ConfigureAwait(false) pose any problems or will the flowing of ExecutionContext's logical call context take care of me there? (my reading and testing so far indicates that it will). Example WebAPI Controller Action: [CustomAuthorizeThatSetsCurrentUsersClaimsToThreadCurrentContextAndHttpContextCurrentUser] public async Task<Order> Get(int orderId) { return await _orderBusinessLogicLibrary.LoadAsync(orderId); // defaults to .ConfigureAwait(true) } Example load functions from external, referenced library:

In the context of ASP.NET, why doesn't Task.Run(…).Result deadlock when calling an async method?

泄露秘密 提交于 2019-12-03 08:01:36
I created a simple WebApi project with a single controller and a single method: public static class DoIt { public static async Task<string> GetStrAsync(Uri uri) { using (var client = new HttpClient()) { var str = await client.GetStringAsync(uri); return str; } } } public class TaskRunResultController : ApiController { public string Get() { var task = Task.Run(() => DoIt.GetStrAsync(new Uri("http://google.com")) ); var result = task.Result; return result; } } I have a good understanding of async/await and tasks; almost religiously following Stephen Cleary. Just the existence of .Result makes me

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

試著忘記壹切 提交于 2019-12-02 20:49:32
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 floating around here and there so as to also provide valuable information. My question, however, is right under "What I would like to know". I'm on a mission to finally understand the various ways offered by .NET to invoke a delegate on a specific thread. What I would like to know: I am looking for the most general way possible (that is not Winforms or

When does Task.Run flow SynchronizationContext with ExecutionContext?

那年仲夏 提交于 2019-12-02 02:25:47
问题 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