Click here to Skip to main content
Rate this: bad
good
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
   {
      ph.Write(false);
      Thread.Sleep(2000);
      ph.Write(true);
      Thread.Sleep(5 * MinuteMs);
   }
}
 
I'm a new and a little lost how to do it
Posted 4-Dec-12 2:08am
ve3tru219
Edited 4-Dec-12 6:01am
v2

1 solution

Rate this: bad
good
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.
  Permalink  
Comments
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 OriginalGriff 295
1 DamithSL 265
2 CPallini 235
3 Maciej Los 190
4 George Jonsson 170
0 OriginalGriff 5,415
1 DamithSL 4,422
2 Maciej Los 3,820
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,911


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 4 Dec 2012
Copyright © CodeProject, 1999-2014
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