C# 事件在错误的线程上触发

发布于 2024-10-18 22:06:54 字数 774 浏览 3 评论 0原文

有一个应用程序,允许用户与多个串行设备进行通信。 管理器类用于启动应用程序,该应用程序为每个串行设备创建一个新线程。在此线程内,创建串行设备,并将线程和串行设备对象存储在管理器类内,以供以后需要时使用。

然后,串行设备类在它自己的新线程上创建一个 com 端口类,用于连接到 com 端口并发送/接收数据。当接收到数据时,串行设备类会触发一个事件,而串行设备类又会向管理器类触发一个事件,管理器类又会向 UI 触发一个事件,以提醒用户新数据已到达。

我的问题是,当 com 端口类触发它的事件来通知串行端口类时,串行端口类接收该事件并继续在 com 端口线程下处理。同样,如果用户将任何信息发送到 com 端口,它都会在 UI 线程下运行。

稍后我将添加代码作为编辑,但现在,如果有人能发现任何明显的东西,我将非常感激。

我尝试在串行设备类中接收事件,然后调用一个方法来查看是否使其在正确的线程下运行,但这并不好。

我知道串行设备线程正在运行,就像我在创建它的 com 端口类后在类中执行 Application.Run 一样。

我没有使用任何后台工作人员,因为这些线程意味着在应用程序的生命周期中存在,并且我了解后台工作人员意味着用于短期运行的计算。

非常感谢

编辑:

忘记提及,这是.NET 2.0中的Winforms应用程序,因此没有可用的调度程序

编辑: 好的,看来信息是在 DataReceived 线程内传递的(我认为它也不是 com 端口线程)。 我还尝试使用 BackgroundWorker 作为串行设备类,但这也没有任何区别。 帮助?

All

I have an application that allows a user to communicate with multiple serial devices.
A manager class is used to start the application, which creates a new thread for each serial device. Inside this thread, the serial device is created and the thread and serial device object are stored inside the manager class for later when needed.

The serial device class then creates a com port class on it's own new thread which is used to connect to the com port and send / receive data. When data is received an event is fired up to the serial device class which in turn fires an event to the manager class, which in turn fires an event to the UI to alert the user that new data has arrived.

My problem comes that when the com port class fires it's event to notify the serial port class, the serial port class receives the event and continues processing under the com port thread. Likewise if the user sends any information down to the com port, it all runs under the UI thread.

I will add code as an edit later, but for now, if anyone can spot anything obvious I would be seriously greatful.

I have tried receiving the event in the serial device class and then invoking a method to see if that makes it run under the correct thread but that was no good.

I know the serial device thread is running as I do an Application.Run inside the class after it's created it's com port classes.

I'm not using any background workers as these threads are meant to exist for the life of the application and I understand background workers are meant to be used for short running calculations.

Many thanks

EDIT:

Forgot to mention, this is a Winforms app in .NET 2.0, so no Dispatcher available

EDIT:
Okay, so it looks like that the information is being passed up inside the DataReceived thread (I think as it isn't the com port thread either).
I also tried using a BackgroundWorker for the serial device class but this also didn't make any difference.
Help?

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

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

发布评论

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

评论(2

半葬歌 2024-10-25 22:06:54

.NET 2.0 有 SynchronizationContext。这是一个轻微的痛苦(你必须将上下文从 UI 线程传递到其他线程),但它应该可以解决问题。

更多信息请参见:http://msdn.microsoft.com/en-us/magazine /gg598924.aspx

.NET 2.0 has SynchronizationContext. It's a slight pain (you have to pass the context from the UI thread to the others) but it should do the trick.

More info here: http://msdn.microsoft.com/en-us/magazine/gg598924.aspx

无名指的心愿 2024-10-25 22:06:54

听起来好像您需要创建一种消息传递机制来将消息从 UI 线程发送到正确的端口线程。队列可能会起作用。让 UI 线程将消息发布到队列上,并让端口线程处理这些消息。

从端口线程切换到 UI 线程可以使用 Dispatcher.CurrentDispatcher 来完成,

这是您正在寻找的吗?

编辑 我假设一个 WinForms 应用程序

It sounds as if you need to create a kind of messaging mechanism to send messages from the UI thread to the correct port-thread. A queue might work. Let the UI thread post messages on the queue and have the port-thread process these messages.

Switching from the port-thread to the UI thread can be done using Dispatcher.CurrentDispatcher

Is this what you are looking for?

EDIT I am assuming a WinForms application

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