单独的线程仅与DoEvents一起运行 [英] Separate threads only run with DoEvents

查看:69
本文介绍了单独的线程仅与DoEvents一起运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我之前已经拿过这个,但我最后没有得到答案

时间,所以希望这次我会有更好的运气。

我从主线程的串口发送数据。我等待

后台线程的回复。当我的主线程正在等待(仅100ms)时,它位于

a循环中,调用DoEvents并测试标志。


后台线程查看传入数据,当它识别

时会设置一个标志。


同时,主线程看到标志已设置并处理

回复。一切都很好。


但是如果我用Thread.Sleep(10)替换对DoEvents的调用,它就不再工作

了。后台线程永远不会嗅到数据。为什么

是?我认为多线程的重点在于你不需要为了让另一个线程获得处理时间而获得
''yield''。


顺便说一句,请随意批评我所描述的整体技术

。我自己并不是很满意,所以任何建议都是欢迎的。


TIA


Charles

I know I have brought this one up before, but I didn''t get an answer last
time, so hopefully I will have better luck this time.

I send data out of a serial port on my main thread. I wait for a response on
a background thread. While my main thread is waiting (only 100ms) it sits in
a loop calling DoEvents and testing a flag.

The background thread looks at the incoming data, and when it recognises
something it sets a flag.

Meanwhile, the main thread sees that the flag has been set and processes the
response. All well and good.

But if I replace the call to DoEvents with Thread.Sleep(10) it doesn''t work
anymore. The background thread never gets a sniff at the data. Why would
that be? I thought the point of multi-threading was that you didn''t need to
''yield'' to allow another thread to get processing time.

Incidentally, please feel free to criticise the overall technique I have
described. I''m not totally happy with it myself, so any suggestions are
welcome.

TIA

Charles

推荐答案

Charles,


后台线程如何接收数据。它是以某种方式连接到UI-Thread的消息泵吗?


Klaus


" Charles法律与QUOT; < BL *** @ nowhere.com> schrieb im Newsbeitrag

新闻:%2 **************** @ tk2msftngp13.phx.gbl ...
Charles,

how does the background thread receive the data. Is it somehow connected to
the message pump of the UI-Thread?

Klaus

"Charles Law" <bl***@nowhere.com> schrieb im Newsbeitrag
news:%2****************@tk2msftngp13.phx.gbl...
我知道我以前带过这个,但我最后没有得到答案,所以希望这次我会有更好的运气。

我从串口发送数据在我的主线程上。我在后台线程上等待响应
。当我的主线程正在等待(仅100ms)时,它在一个循环中调用DoEvents并测试一个标志位于


后台线程查看传入的数据,当它识别出
它设置了一个标志。

同时,主线程看到标志已经设置并处理
响应。一切都很好。

但如果我用Thread.Sleep(10)替换对DoEvents的调用,它就不再是
了。后台线程永远不会嗅到数据。为什么会这样?我认为多线程的重点是你不需要
来收益以允许另一个线程获得处理时间。

顺便说一下,请随意批评我所描述的整体技术。我自己并不是很满意,所以欢迎任何建议。

TIA

Charles
I know I have brought this one up before, but I didn''t get an answer last
time, so hopefully I will have better luck this time.

I send data out of a serial port on my main thread. I wait for a response on a background thread. While my main thread is waiting (only 100ms) it sits in a loop calling DoEvents and testing a flag.

The background thread looks at the incoming data, and when it recognises
something it sets a flag.

Meanwhile, the main thread sees that the flag has been set and processes the response. All well and good.

But if I replace the call to DoEvents with Thread.Sleep(10) it doesn''t work anymore. The background thread never gets a sniff at the data. Why would
that be? I thought the point of multi-threading was that you didn''t need to ''yield'' to allow another thread to get processing time.

Incidentally, please feel free to criticise the overall technique I have
described. I''m not totally happy with it myself, so any suggestions are
welcome.

TIA

Charles


嗨克劳斯


它调用WaitCommEvent,然后使用INFINITE作为

第二个参数的WaitForSingleObject。


它打开的线程是在打开端口时启动的,因此它始终是

查找数据。线程被标记为IsBackground = True。


Charles

" Klaus L?ffelmann" < FO *********** @ loeffelmann.de>在消息中写道

news:OM ************** @ tk2msftngp13.phx.gbl ...
Hi Klaus

It calls WaitCommEvent and then WaitForSingleObject with INFINITE as the
second parameter.

The thread that it is on is started when the port is opened, so it is always
looking for data. The thread is marked as IsBackground = True.

Charles
"Klaus L?ffelmann" <fo***********@loeffelmann.de> wrote in message
news:OM**************@tk2msftngp13.phx.gbl...
Charles,
后台线程如何接收数据。它是否以某种方式将
连接到UI-Thread的消息泵?

Klaus

Charles Law < BL *** @ nowhere.com> schrieb im Newsbeitrag
新闻:%2 **************** @ tk2msftngp13.phx.gbl ...
Charles,

how does the background thread receive the data. Is it somehow connected to the message pump of the UI-Thread?

Klaus

"Charles Law" <bl***@nowhere.com> schrieb im Newsbeitrag
news:%2****************@tk2msftngp13.phx.gbl...
我知道我带来了这个之前有一个,但我上次没有得到
的答案,所以希望这次我会有更好的运气。

我从主线程的串口发送数据。我在
I know I have brought this one up before, but I didn''t get an answer last time, so hopefully I will have better luck this time.

I send data out of a serial port on my main thread. I wait for a
后台线程上等待


响应。当我的主线程正在等待(仅100ms)时,

a background thread. While my main thread is waiting (only 100ms) it


位于

一个调用DoEvents并测试标志的循环。

后台线程看起来在传入的数据中,当它识别出某个东西时,它会设置一个标志。

同时,主线程看到该标志已经设置并处理
a loop calling DoEvents and testing a flag.

The background thread looks at the incoming data, and when it recognises
something it sets a flag.

Meanwhile, the main thread sees that the flag has been set and processes


响应。一切都很好。

但如果我用Thread.Sleep(10)替换对DoEvents的调用,它就不会
response. All well and good.

But if I replace the call to DoEvents with Thread.Sleep(10) it doesn''t


工作

了。后台线程永远不会嗅到数据。为什么会这样?我认为多线程的重点是你不需要
anymore. The background thread never gets a sniff at the data. Why would
that be? I thought the point of multi-threading was that you didn''t need


''yield''以允许另一个线程获得处理时间。

顺便说一句,请随意批评我所描述的整体技术。我自己并不是很满意,所以欢迎任何建议。

TIA

Charles
''yield'' to allow another thread to get processing time.

Incidentally, please feel free to criticise the overall technique I have
described. I''m not totally happy with it myself, so any suggestions are
welcome.

TIA

Charles



希望这个链接可以帮到你:

http://www.devx.com/getHelpOn/10MinuteSolution/20365

Hope this link will help you :

http://www.devx.com/getHelpOn/10MinuteSolution/20365


这篇关于单独的线程仅与DoEvents一起运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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