从多个线程发射事件 [英] Firing events from multiple threads

查看:73
本文介绍了从多个线程发射事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类将数据写入串口,并有一个单独的线程

来读取同一端口的数据。通常,主线程将请求

写入串口,然后返回给调用者。当应用程序继续运行时,读取线程等待响应

。当收到回复

时,我想发起一个事件。但是,我不希望读取线程

引发事件,因为该事件可能会由应用程序产生一整套新处理

。我希望读取线程缓冲数据,然后再回到阅读中。我怎样才能发出主线程信号来提示事件

数据被缓冲?

I have a class that writes data to the serial port and has a separate thread
to read data from the same port. Typically the main thread writes requests
to the serial port and then returns to the caller. The read thread waits for
a response while the application continues running. When a response is
received, I want to fire an event. But, I don''t want the read thread to
raise the event because the event could spawn a whole set of new processing
by the application. I''d like the read thread to buffer the data and then go
back to reading. How can I signal the main thread to raise the event once
the data is buffered?

推荐答案

查看ManualResetEvent或AutoResetEvent
http://msdn.microsoft.com/library/de...classtopic.asp

" TrtnJohn" < Tr的****** @ discussions.microsoft.com>在消息中写道

新闻:5F ********************************** @ microsof t.com ...
Look in ManualResetEvent or AutoResetEvent

http://msdn.microsoft.com/library/de...classtopic.asp

"TrtnJohn" <Tr******@discussions.microsoft.com> wrote in message
news:5F**********************************@microsof t.com...
我有一个类将数据写入串口,并有一个单独的
线程来读取同一端口的数据。通常,主线程将请求写入串口,然后返回给调用者。在应用程序继续运行时,读取线程等待
响应。收到回复后,我想发起一个事件。但是,我不希望读取线程引发事件,因为事件可能会由应用程序产生一整套新的
处理。我希望读取线程缓冲数据,然后再回到阅读状态。如果数据被缓冲,我如何发信号通知主线程引发事件?
I have a class that writes data to the serial port and has a separate
thread
to read data from the same port. Typically the main thread writes
requests
to the serial port and then returns to the caller. The read thread waits
for
a response while the application continues running. When a response is
received, I want to fire an event. But, I don''t want the read thread to
raise the event because the event could spawn a whole set of new
processing
by the application. I''d like the read thread to buffer the data and then
go
back to reading. How can I signal the main thread to raise the event once
the data is buffered?



谢谢一些人。但是,如果我不希望我的主线程阻止等待事件发生。我希望通知是异步的。否则,

应用程序将不再处理窗口消息,而我的班级正在等待

作为回复。


有些盖伊"写道:
Thanks Some Guy. But, if I don''t want my main thread to block waiting for
the event. I''d like the notification to be asynchonous. Otherwise, the
application will no longer process window messages while my class is waiting
for a response.

"Some Guy" wrote:
查看ManualResetEvent或AutoResetEvent

http://msdn.microsoft.com/library/de...classtopic.asp

TrtnJohn < Tr的****** @ discussions.microsoft.com>在消息中写道
新闻:5F ********************************** @ microsof t.com。 ..
Look in ManualResetEvent or AutoResetEvent

http://msdn.microsoft.com/library/de...classtopic.asp

"TrtnJohn" <Tr******@discussions.microsoft.com> wrote in message
news:5F**********************************@microsof t.com...
我有一个类将数据写入串口,并有一个单独的
线程来从同一个端口读取数据。通常,主线程将请求写入串口,然后返回给调用者。在应用程序继续运行时,读取线程等待
响应。收到回复后,我想发起一个事件。但是,我不希望读取线程引发事件,因为事件可能会由应用程序产生一整套新的
处理。我希望读取线程缓冲数据,然后再回到阅读状态。如果数据被缓冲,我怎样才能发出主线程信号以引发事件?
I have a class that writes data to the serial port and has a separate
thread
to read data from the same port. Typically the main thread writes
requests
to the serial port and then returns to the caller. The read thread waits
for
a response while the application continues running. When a response is
received, I want to fire an event. But, I don''t want the read thread to
raise the event because the event could spawn a whole set of new
processing
by the application. I''d like the read thread to buffer the data and then
go
back to reading. How can I signal the main thread to raise the event once
the data is buffered?




提示: Control.Invoke方法(委托)。

如果您了解Control.Invoke方法

(代表),我认为您可以弄明白。


" TrtnJohn"写道:
Hint: Control.Invoke Method (Delegate).
I think you can figure it out if you understand Control.Invoke Method
(Delegate).

"TrtnJohn" wrote:
我有一个类将数据写入串口,并有一个单独的线程来读取同一端口的数据。通常,主线程将请求写入串口,然后返回给调用者。在应用程序继续运行时,读取线程等待响应。收到回复后,我想发起一个事件。但是,我不希望读取线程引发事件,因为事件可能会由应用程序产生一整套新的处理。我希望读取线程缓冲数据,然后返回阅读。一旦数据被缓冲,如何通知主线程引发事件?
I have a class that writes data to the serial port and has a separate thread
to read data from the same port. Typically the main thread writes requests
to the serial port and then returns to the caller. The read thread waits for
a response while the application continues running. When a response is
received, I want to fire an event. But, I don''t want the read thread to
raise the event because the event could spawn a whole set of new processing
by the application. I''d like the read thread to buffer the data and then go
back to reading. How can I signal the main thread to raise the event once
the data is buffered?



这篇关于从多个线程发射事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆