-
OverviewA lot of effort is spent marshalling UI control updates to the UI thread. I am wondering (proposing?) that the Control base class is modified to handle these details internally, automatically—but leaves existing functionality (Invoke and BeginInvoke) in place. (So, hypothetically, a non-breaking change.) I would like to continue the discussion that was started here: #4631 ProposalControls are already aware of their UI thread (and expose it via InvokeRequired) and have a built-in means of searching for it as part of Invoke and BeginInvoke. I'm proposing that instead of WinForms developers writing something like this:
That they could instead just mutate the Property as they would normally (on the UI thread):
I am not proposing any changes to InvokeRequired/Invoke/BeginInvoke. I think they all have a purpose, and being able to execute a Delegate (or an Action directly, as proposed in #4608) makes sense. Why?There is a lot of confusion around this subject, I tried to expound some of them in the discussion in #4631. If what I'm proposing is feasible, then I think it would greatly simplify mutating a UI Control. Developers can access a Control's Property normally ( In situations where a developer attempts to update a UI Control improperly right now ( Further, there are a number of .NET constructs and recommendations that are predicated on marshalling UI Controls to update on the UI thread—and I think this proposal could eliminate the need for some of these.
This proposal also solves for the cases when best practices aren't possible (for whatever reason). For example, the best practice is to always update the UI from the UI thread:
But when that isn't possible, and something like this occurs, it wouldn't be an issue:
I think it's particularly beneficial inside of an async method, because there is no guarantee that the State Machine is running on the Threadpool, and it could be running on the UI thread—so code written like the above example may execute just fine on the developer's computer repeatedly, only to fail later on. Is this proposal logical, feasible, technically correct?I feel like what I'm suggesting is too common-sense to not have been implemented a long time ago, and so I doubt the veracity of my claims. If this proposal makes sense, I'm willing to work on it. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
You cannot eliminate that, it is the framework-independent API for doing thread switches, in e.g. libraries which aren't tied to WinForms only but can be used in any context. In fact much of the .NET runtime makes use of SynchronizationContext in this framework independent manner, in particular it is why when you
While this proposal tries to solve one problem, I have concerns with new problems being introduced:
Threading is hard, this proposal will remove one case of complexity (manually switching threads) by trading it off for a different complexity (deadlocks become more common). I'm not entirely opposed but I personally don't feel the "it just works" experience is worth the decrease in quality. The current clear exception meant bugs usually got fixed, deadlocks have no stack trace and will need to be reproduced, meaning less deadlocks will actually get fixed, resulting in decreased quality of WinForms applications. Also the hidden performance degredation you get when you use this feature means you have to spend more time optimizing your call sites, by replicating what you currently have to do anyways, switching the thread manually. Personally I prefer the current behavior, the exception is deterministic (even if its only in debug builds) and has a documented solution. |
Beta Was this translation helpful? Give feedback.
You cannot eliminate that, it is the framework-independent API for doing thread switches, in e.g. libraries which aren't tied to WinForms only but can be used in any context. In fact much of the .NET runtime makes use of SynchronizationContext in this framework independent manner, in particular it is why when you
await
something you will continue on the right thread and don't have to worry about thread switches happening behind your back.