C# 中的 STAThread 和进程输出捕获
这是我遇到的一个奇怪的问题。我有一个用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
UI 线程需要 STATHREAD。来自 MSDN:
此外,Windows 窗体应用程序将只有一个 UI 线程。听起来,要拥有响应式 UI,您可能需要做的是使用后台工作线程来控制脚本的运行,并将更改传达回 UI 线程。
STATHREAD is required for a UI thread. From MSDN:
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.
我不知道在任何情况下
[STAThread]
属性都会导致线程具有更高的优先级。从这个SO问题讨论STA和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: