Click here to Skip to main content
13,090,519 members (55,218 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I have a program that's having problems.
Its suppose to read the input from a controller then when the controller is on, it does X in a loop checking every 5mins.
Problem is I get faulty readings from the controller every now and then.
So what I want to do, is add a little counter so it checks it 4 times
and only when it gets 4 in a row it does x.
something like for (int i = 0; i < 4; i++)

into this

public static void Method_6()
   Thread.Sleep(2 * HourMs);
   while((method_state_6) && (!phcontroller.Read())) //phcontroller is just a relay that closes when needed
      Thread.Sleep(5 * MinuteMs);

I'm a new and a little lost how to do it
Posted 4-Dec-12 1:08am
Updated 4-Dec-12 5:01am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You can try something like:
int i = 0;
bool flag = true;
while (i < 4 && flag)
flag = inputSource;
if (flag)
do X

If flag is true 4 times in a row, it will perform X.
ve3tru 4-Dec-12 16:41pm
OK thanks but I'm a bit of a newb and not sure I understand your whole code.
flag = inputSource; lost me there whats an inputSource
also I guess I need it to wait some between checks. Maybe a min. or so to make sure its not still in a glitch.
thanks for the help
wizardzz 4-Dec-12 16:52pm
I meant by "inputsource" as your check, assuming it returns boolean (that is what I interpreted by a check). Just put a sleep in the while loop.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.170813.1 | Last Updated 4 Dec 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100