无限循环处理 [英] Infinite loop handling

查看:80
本文介绍了无限循环处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,
我有一个循环,用于为某个值轮询一个字节,
while(receivebyte == null);
直到设备发送字节为止.我想知道如何修改代码,以便可以在指定的持续时间内运行此循环,然后循环结束.
谢谢

Hi Dears,
I have a loop for poling a byte for some value,
while(receivebyte==null);
it is to stay untill device sends the byte. I want to know how I can modify the code so I may run this loop for specified duration after which the loop goes to end.
thank you

推荐答案

我将使用Monitor.Wait并将其设置为超时.将receivebyte逻辑放入单独的线程中,然后使用Monitor对其进行控制.看一下Monitor.Wait和Monitor.Pulse(如果收到等待的字节,则需要Pulse来唤醒"主线程备份).
I would use Monitor.Wait and set it to timeout. Put the receivebyte logic into a separate thread, then use the Monitor to control it. Take a look at Monitor.Wait and Monitor.Pulse (you''ll need Pulse to "wake" the master thread back up if you receive the byte you are waiting for).


if您不想使用单独的线程,则可以修改代码,例如以下

if you don''t want to use a separate thread then you can modify your code like following

time_out=SOME_TIME_OUT_VALUE;
while((time_out-- > 0) && (receivebyte == null));



请注意,这并不保证您会在特定时间超时(您可能希望为此使用时间检查).相反,它只是声明在达到特定的循环计数后您将超时.



Note that this doesn''t guarantee that you will timeout at a specific time (you''d probably want to use a time check for that). Rather, it simply states that you will timeout after a particular loop count is reached.


这篇关于无限循环处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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