TPL 与 InvokeRequired/Invoke
从 .NET 4.0 开始,就有了 TPL 来执行异步任务。如果您正在阅读 msdn,所有与表单/UI 交互的异步操作仍然使用 InvokeRequire ... Invoke() 模式。 我想问的是,这其中有原因吗?根据我的了解,TPL 应该是旧线程机制的替代品。那么当涉及到 UI 线程时忽略它有什么意义呢? 对此有什么想法吗?
Since .NET 4.0 there is the TPL to execute asynchronous tasks. If you are reading the msdn, all async operations interacting with forms / UI still uses the InvokeRequire ... Invoke() pattern.
What I'm asking is that is there a reason for it? From what I learned, TPL should be sort of a replacement of older threading mechanisms. So what's the point to ignore it when it's about UI threading?
Any thoughts about that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这似乎相当主观......
当您说“自 .NET 4.0 以来”时,您是在说“截至今年 4 月” - .net 已经存在 10 年了,并且 InvokeRequired/Invoke 已经使用了过去 9 年为什么 MS 会出于某种原因破坏所有现有的 UI 代码?即使存在调用线程的新方法,他们也不能简单地修改模式而不担心兼容性问题。
此外,TPL 与 InvokeRequired/Invoke 并不类似 - TPL 是关于简单的并行性,而调用是关于在特定线程上运行代码。我不确定即使没有兼容性问题,为什么一个会取代另一个。
请注意,没有什么可以阻止您使用 TPL 来确保您在正确的线程上调用 UI 组件。事实上,您可以轻松做到这一点。但这取决于您,当前的 API 不会以不向后兼容的方式进行更改。
This seems fairly subjective...
When you say "Since .NET 4.0", you are saying "as of April of this year" - .net has been around for 10 years now, and InvokeRequired/Invoke has been used for the last 9. Why would MS break all existing UI code for any reason? Even if a new way of calling to a thread existed, they could not simply modify the pattern without huge compatibility concerns.
Also, the TPL is not analogous to InvokeRequired/Invoke - the TPL is about easy parallelism, and invoke is about running code on a specific thread. I'm not sure why one would replace the other even if there were no compatibility concerns.
Note that there is nothing stopping you from using the TPL to ensure you call UI components on the correct thread. In fact, you can easily do this. But that is up to you and the current API is not going to change in a way that isn't backwards compatible.
通过 TPL,您可以使用 TaskScheduler 指定目标线程。 FromCurrentSynchronizationContext 该方法指定 Task 将在主线程上执行。我建议使用它而不是 Invoke。
With TPL you can specify target thread using TaskScheduler.FromCurrentSynchronizationContext this method specify that Task will be executed on main thread. I recommend using it instead of Invoke.
这里的问题是什么? TPL 的存在并没有改变 UI 本质上是单线程的事实,并且要求只能在 UI 线程上访问控件。 (这是 Windows 的限制,而不是 .NET UI 框架的限制。TPL 无法改变几十年来的 Windows 设计限制。)
如果您的问题是关于将任务与 InvokeRequired/Invoke 混合,那么有一种比 TPL 更面向 TPL 的方法调用。 TPL 提供了一种内置方法来安排延续任务在 UI 线程上运行。因此,您将面向后台的工作放在一个任务中,然后在另一个任务中更新 UI。请参阅这篇文章任务调度程序和 SynchronizationContext。
(但实际上,TPL 不会取代任何旧的线程 API。如果 Invoke 是完成您想要做的事情的最佳方式,请使用它。)
What is the question here? The existence of TPL doesn't change the fact that UIs are inherently single-threaded, and require controls to be accessed only on the UI thread. (And that's a Windows limitation, not a limitation of the .NET UI frameworks. TPL can't change decades of Windows design limitations.)
If your question is about mixing Tasks with InvokeRequired/Invoke, there is a more TPL-oriented way than Invoke. TPL comes with a built-in way to schedule a continuation Task to run on the UI thread. So you put your background-oriented work in one Task, and then your UI updates in another Task. See this post on task schedulers and SynchronizationContext.
(But really, TPL doesn't replace any of the older thread APIs. If Invoke is the best way to do what you're trying to do, use it.)