Click here to Skip to main content
11,708,312 members (63,558 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C#
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
Edited 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 at 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 at 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
0 Maciej Los 220
1 OriginalGriff 183
2 Mika Wendelius 180
3 Sergey Alexandrovich Kryukov 149
4 Peter Leow 130
0 OriginalGriff 9,253
1 Sergey Alexandrovich Kryukov 8,562
2 CPallini 5,189
3 Maciej Los 4,966
4 Mika Wendelius 3,856

Advertise | Privacy | Mobile
Web01 | 2.8.150819.1 | Last Updated 4 Dec 2012
Copyright © CodeProject, 1999-2015
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