为什么 WPF 需要将 STAThread 属性应用于 Main 方法?

发布于 2024-08-02 08:32:21 字数 141 浏览 8 评论 0原文

我是 WPF 新手,在我阅读的每个教程中,他们要么将 [System.STAThread] 属性应用于其 Main 方法,要么告诉读者执行以下操作那。

这个属性真的是“必需的”吗?如果是这样,为什么?

I am new to WPF, and in every tutorial I read, they either have a [System.STAThread] attribute applied to their Main method, or they tell the reader to do that.

Is this attribute really "required"? And if so, why?

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

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

发布评论

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

评论(2

柠檬心 2024-08-09 08:32:21

这更多的是 Windows 的要求,而不是 WPF 的要求,并且可以追溯到 .NET 之前的 Windows 窗体和控件的原始设计。

STAThread 指“单线程公寓”,指当前(主)线程使用的线程模型。使用的线程模型决定了其他 .NET 和 COM 应用程序如何与您的应用程序(本质上是其线程)进行通信。与 MTA 线程模型不同,单线程应用程序模型要求单个对象不能同时“驻留在”多个 STA 线程中;并允许仅通过编组为对象在公寓之间传递指向数据的指针。

基本上,通过 [STAThread] 声明,其他应用程序在向您发送数据时就会知道您的线程的策略是什么。 STA模型是Windows线程/应用程序最常见的线程模型;但有时您会遇到某些代码,如果从 STA 模型线程调用,这些代码将无法运行,因为它被设计为以不符合 STA 限制的方式跨线程边界发送/接收数据。预先了解给定线程的单元模型允许 IDE 在编译时捕获这些异常,而不是在运行时尝试跨线程边界使用对象时出现令人讨厌的访问冲突错误。

您可以从 MSDN 文章中了解 STA 和 MTA 线程: http://msdn.microsoft.com/en-us/library/ms680112(VS.85).aspx

请注意,即使是普通的 .NET 应用程序(WPF 之前的版本)也需要在主线程之上声明 [STAThread] ()。

This is more a Windows requirement than a WPF one, and goes back to the original design of Windows forms and controls, from before .NET.

STAThread refers to "Single-Threaded Apartments" which refers to the threading model used by the current (main) thread. The threading model in use dictates how other .NET and COM applications will talk to your application (and inherently, its threads). The single-threaded application model requires that no single object "live in" more than one STA thread at a time, verses the MTA thread model; and allows for the passing of pointers to data across apartments only via marshalling-as-object.

Basically, with the [STAThread] declaration, other applications will know what your thread's policy is when sending you data. The STA model is the most common threading model for Windows threads/applications; but you'll sometimes come across certain code that won't run if called from an STA-modeled thread, because it's designed to send/receive data across thread boundaries in ways that don't comply with the STA restrictions. Knowing beforehand what the apartment model of a given thread allows the IDE to catch these exceptions at compile-time instead of getting nasty access violation errors when you attempt to use an object across thread boundaries during runtime.

You can read about STA and MTA threads from the MSDN article at: http://msdn.microsoft.com/en-us/library/ms680112(VS.85).aspx

Note that even normal .NET applications (from before WPF) required the [STAThread] declaration atop of the main().

梦里°也失望 2024-08-09 08:32:21

博客条目对此有一个很好的答案。

引用自博客:

STAThreadAttribute
申请后,它改变了公寓
当前线程的状态为
单线程。无需进入
关于 COM 的大量讨论和
线程,该属性确保
之间的沟通机制
当前线程和其他线程
可能想通过 COM 与其交谈。什么时候
您使用的是 Windows 窗体,具体取决于
根据您正在使用的功能,可能是
使用 COM 互操作
与操作系统通信
成分。很好的例子是
剪贴板和文件对话框。

Windows 窗体不受支持
MTA 或自由线程单元。
使用 Windows 窗体的应用程序
应始终申报公寓
他们正在使用的风格,就像其他一些风格一样
组件可以初始化
线程的单元状态不正确。

There's an excellent answer for this in this blog entry.

Quoting from the blog:

When the STAThreadAttribute is
applied, it changes the apartment
state of the current thread to be
single threaded. Without getting into
a huge discussion about COM and
threading, this attribute ensures the
communication mechanism between the
current thread and other threads that
may want to talk to it via COM. When
you're using Windows Forms, depending
on the feature you're using, it may be
using COM interop in order to
communicate with operating system
components. Good examples of this are
the Clipboard and the File Dialogs.

Windows Forms is not supported within
a MTA or free threaded apartment.
Applications using Windows Forms
should always declare the apartment
style they're using, as some other
component could initialize the
apartment state of thread improperly.

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