synchronizationcontext

Why is SynchronizationContext.Current null in my Winforms application?

左心房为你撑大大i 提交于 2019-12-18 04:31:26
问题 I just wrote this code: System.Threading.SynchronizationContext.Current.Post( state => DoUpdateInUIThread((Abc)state), abc); but System.Threading.SynchronizationContext.Current is null 回答1: See this explanation. SynchronizationContext.Current is only set in the main thread (which is the only thread where you don't actually need it) The blog post proposes a workaround. 回答2: To get it to work. In your class private SynchronizationContext synchronizationContext; In the UI thread (main thread)

Calling an async method with c#5.0

随声附和 提交于 2019-12-12 17:44:50
问题 I do some tests with the new asynchronous pattern of C# 5.0 (async/await) I have a problem with understanding how the asynchronous methods are called. Considering this code : private async Task<string> DownloadAsync() { progress.ProgressChanged += (s, e) => { progressBar1.Value = e.value; }; return await DownloadSomething(myurl, progress); } private async void CallDownloadAsync() { string text = await DownloadAsync(); progressBar1.Value = 0; label1.Text = "Done!"; } private void button4_Click

Alternative of Dispatcher class (from .net 3.0) to use in .net 2.0 context

老子叫甜甜 提交于 2019-12-11 04:46:47
问题 I need an alternative for Dispatcher (.net 3.0) to use for a windows service (done in .net 2.0). Can you give me some idea how to achieve something like that or point me some links? I know that a dispatcher has a SynchronizationContext behind, but I don't know how I can use a SynchronizationContext into a service. If you think that I should stick to the Dispatcher (.net 3.0) ... how can I manipulate it ( OnServiceStop , OnServiceStart ) edited: More details (see also...here) Idea is that I

Why does NotifyIcon not set SynchronizationContext?

南楼画角 提交于 2019-12-11 03:15:26
问题 Consider this WinForms program: Module Main Dim notifyicon As New System.Windows.Forms.NotifyIcon 'Dim dummycontrol As New System.Windows.Forms.Control Public Sub Main() If (System.Threading.SynchronizationContext.Current Is Nothing) Then MessageBox.Show("Nothing") Else MessageBox.Show("Something") End If End Sub End Module NotifyIcon is a WinForm control, and requires a message loop, so why will declaring dummycontrol (or any WinForms control) set a SynchronizationContext, but the NotifyIcon

Deadlock with async/await

妖精的绣舞 提交于 2019-12-10 18:46:18
问题 Suppose I'm writing a custom MVC filter which does some asynchronous calls within the method overrides, like so: public class MyActionFilter : System.Web.Mvc.ActionFilterAttribute { public override void OnActionExecuted(ActionExecutedContext ctx) { var stuff = ConfigureAwaitHelper1().Result; // do stuff } public override void OnActionExecuting(ActionExecutingContext ctx) { var stuff = ConfigureAwaitHelper2().Result; // do stuff } private async Task<string> ConfigureAwaitHelper1() { var result

A case when ConfigureAwait(false) causes an error instead of deadlock

徘徊边缘 提交于 2019-12-10 14:10:47
问题 Suppose I have written a library which relies on async methods: namespace MyLibrary1 { public class ClassFromMyLibrary1 { public async Task<string> MethodFromMyLibrary1(string key, Func<string, Task<string>> actionToProcessNewValue) { var remoteValue = await GetValueByKey(key).ConfigureAwait(false); //do some transformations of the value var newValue = string.Format("Remote-{0}", remoteValue); var processedValue = await actionToProcessNewValue(newValue).ConfigureAwait(false); return string

Comparing SynchronizationContext

若如初见. 提交于 2019-12-10 02:50:08
问题 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

When to call SynchronizationContext.SetSynchronizationContext() in a UI application?

跟風遠走 提交于 2019-12-06 21:08:18
问题 I'm learning about the SynchronizationContext class. I'm trying to understand what are the common usage scenarios for calling SynchronizationContext.SetSynchronizationContext() in the context of a WinForm/WPF application. What does it mean to set the SynchronizationContext of a thread? When should I do it and why? Also, if I set it, should I unset it at some point? Edit: In his answer, @Hans Passant asked why I was contemplating SetSynchronizationContext() . The idea I have is to set the

How to get a Synchronization Context for the second form shown

半世苍凉 提交于 2019-12-06 04:42: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 can't. I get the standard "You're not allowed to update the UI from the wrong thread" exception. My code

When to call SynchronizationContext.SetSynchronizationContext() in a UI application?

99封情书 提交于 2019-12-05 05:12:38
I'm learning about the SynchronizationContext class. I'm trying to understand what are the common usage scenarios for calling SynchronizationContext.SetSynchronizationContext() in the context of a WinForm/WPF application. What does it mean to set the SynchronizationContext of a thread? When should I do it and why? Also, if I set it, should I unset it at some point? Edit: In his answer, @Hans Passant asked why I was contemplating SetSynchronizationContext() . The idea I have is to set the context on a worker thread so that code running on that thread will have a context to use. private void