C# 中的 STAThread 和进程输出捕获

发布于 2024-08-25 14:06:23 字数 305 浏览 4 评论 0原文

这是我遇到的一个奇怪的问题。我有一个用 C# 编写的窗口应用程序来进行测试。它有一个 MDI 父窗体,托管一些子窗体。其中一种表单通过创建进程来启动测试脚本并将脚本输出捕获到文本框。另一种形式是打开串行端口并监视我正在使用的设备的状态(如外壳)。如果我同时运行它们,脚本的输出似乎只在测试完成后出现在文本框中。但是,如果我不打开串行端口表单,则会实时捕获脚本的输出。

有谁知道导致问题的原因是什么?我注意到串行端口表单的 onDataReceived eventT 处理程序有一个 [STAThread] 标头。这会导致串口线程的优先级高于其他进程吗?

提前致谢。

This is a strange problem I encountered. I have an window application written in c# to do testing. It has a MDI parent form that is hosting a few children forms. One of the forms launch test scripts by creating processes and capture the scripts output to a text box. Another form open serial port and monitoring the status of the device I am working on(like a shell). If I ran both of them together, the output of the script seems only appear in the text box after the test is done. However, If I don't open the serial port form, the output of the script is captured in real time.

Does anyone knows what's causing the problem? I notice the onDataReceived evenT handler for serial port form has a [STAThread] header to it. Will this cause the serial port thread having higher priority than other processes?

Thanks in advance.

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

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

发布评论

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

评论(2

冷…雨湿花 2024-09-01 14:06:23

UI 线程需要 STATHREAD。来自 MSDN:

表示应用程序的 COM 线程模型是单线程单元 (STA)。

此外,Windows 窗体应用程序将只有一个 UI 线程。听起来,要拥有响应式 UI,您可能需要做的是使用后台工作线程来控制脚本的运行,并将更改传达回 UI 线程。

STATHREAD is required for a UI thread. From MSDN:

Indicates that the COM threading model for an application is single-threaded apartment (STA).

Also, a windows form application will only have a single UI thread. It sounds like what you may need to do to have a responsive UI is use a background worker thread to control running your script, and have it communicate changes back to the UI thread.

╰つ倒转 2024-09-01 14:06:23

我不知道在任何情况下 [STAThread] 属性都会导致线程具有更高的优先级。

从这个SO问题讨论STA和MTA线程之间的差异:

COM 线程模型称为
“公寓”模型,其中执行
初始化 COM 对象的上下文是
与单个线程关联
(单线程公寓)或多个
线程(多线程单元)。在
这个模型,一个 COM 对象,一次
在公寓中初始化,是一部分
该公寓的持续时间
这是运行时。

STA模型用于COM对象
那些不是线程安全的。这意味着
他们不处理自己的事情
同步。这个的一个常见用途
是一个 UI 组件。所以如果另一个
线程需要与
对象(例如按
形式)然后消息被编组
到 STA 线程。窗户形式
消息泵系统就是一个例子
的这个。

如果 COM 对象可以处理它自己的
同步,然后 MTA 模型可以
用于多线程的地方
允许与对象交互
没有编组呼叫。

I don't know of any case where the [STAThread] attribute will cause the thread to have higher priority.

From this SO question that discusses the differences between STA and MTA threads:

The COM threading model is called an
"apartment" model, where the execution
context of initialized COM objects is
associated with either a single thread
(Single Thread Apartment) or many
threads (Multi Thread Apartment). In
this model, a COM object, once
initialized in an apartment, is part
of that apartment for the duration of
it's runtime.

The STA model is used for COM objects
that are not thread safe. That means
they do not handle their own
synchronization. A common use of this
is a UI component. So if another
thread needs to interact with the
object (such as pushing a button in a
form) then the message is marshalled
onto the STA thread. The windows forms
message pumping system is an example
of this.

If the COM object can handle its own
synchronization then the MTA model can
be used where multiple threads are
allowed to interact with the object
without marshalled calls.

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