VS2010 / .NET 4.0中BackgroundWorker线程访问的区别?

发布于 2024-09-01 10:55:01 字数 597 浏览 3 评论 0原文

这是一个有趣的例子 - 在针对 .NET 2.0 / .NET 3.0 / .NET 3.5 运行的 VS2005 / VS2008 中,BackgroundWorker 线程可能不会直接更新启动该线程的 WinForms 表单上的控件 - 您将得到一个 System.来自BackgroundWorker 的InvalidOperationException 指出“跨线程操作无效:从创建它的线程以外的线程访问控制'thecontrol'”。我记得在 2004 年左右,当我第一次开始编写带有后台线程的 .NET WinForms 应用程序时,我就遇到过这个问题。有几种方法可以解决这个问题——这不是一个寻求答案的问题。

最近有人告诉我,现在在 VS2010 / .NET 4.0 中编写时允许进行这种操作。这对我来说似乎不太可能,因为这种对象访问限制一直是线程安全编程的相当基本的部分。允许BackgroundWorker 线程直接访问不属于其本身而是属于其父UI 表单的对象似乎违反了该原则。仔细浏览 .NET 4.0 文档并没有发现任何可以解释这种行为的明显变化。

我没有 VS2010 / .NET 4.0 来测试这一点 - 有权访问该工具集的任何人都可以确定模型是否已更改为允许这种线程交互?我想要么在未来利用它,要么部署线索杆。 ;)

Here's an interesting one - in VS2005 / VS2008 running against .NET 2.0 / .NET 3.0 / .NET 3.5, a BackgroundWorker thread may not directly update controls on a WinForms form that initiated that thread - you'll get a System.InvalidOperationException out of the BackgroundWorker stating "Cross-thread operation not valid: Control 'thecontrol' accessed from a thread other than the thread it was created on". I remember hitting this back in 2004 or so when I first started writing .NET WinForms apps with background threads. There are several ways around the problem - this is not a question asking for that answer.

I've been told recently that this kind of operation is now allowed when written in VS2010 / .NET 4.0. This seems unlikely to me, since this kind of object access restriction has always been a fairly fundamental part of thread-safe programming. Allowing a BackgroundWorker thread direct access to objects owned not by itself but by its parent UI form seems contrary to that principle. A trawl through the .NET 4.0 docs hasn't revealed any obvious changes that could account for this behaviour.

I don't have VS2010 / .NET 4.0 to test this out - does anyone who has access to that toolset know for sure whether the model has changed to allow that kind of thread interaction? I'd like to either take advantage of it in future, or deploy the cluestick. ;)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不;它仍然是一个 InvalidOperationException。

我测试它只是为了确定一下。

No; it's still an InvalidOperationException.

I tested it just to make sure.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文