Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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
Posted

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).
 
Share this answer
 
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.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900