R
R
rinnn2018-09-20 13:39:27
C++ / C#
rinnn, 2018-09-20 13:39:27

Stop if check for a while?

There are several ints that get random values ​​all the time.
For example, at this moment

int 1 = 100
int 2 = 50
int 3 = 150

_________________________________________________________________________________
Cycle example
while (true)
{
    if (int1 > 20)
        {
            //код
            System.Threading.Thread.Sleep(200000);
        }   
    
    if (int2 > 400)
        {
            //код
            System.Threading.Thread.Sleep(200000);
        }

    if (int3 > 300)
        {
            //код
            System.Threading.Thread.Sleep(200000);
        }
}

The loop should always go, BUT if after the condition int1 > 20 is fulfilled, the rest will not be checked, of course, because stream sleeps. There are a lot of such ints, the option with separate threads will not work. I'm just learning, waiting for your advice, thanks in advance.
UPD: A pause is required after executing the code inside the condition

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Satisfied IT, 2018-09-20
@rinnn

It’s a little unclear what you need to get, if you need to check all the conditions, fulfill those that are suitable and then “sleep”, it’s better something like this (but the flows are more adequate)

while (true)
{
    if (int1 > 20)
        {
            //код
        }   
    
    if (int2 > 400)
        {
            //код
        }

    if (int3 > 300)
        {
            //код
        }
   System.Threading.Thread.Sleep(200000);
}

N
Namynnuz, 2018-09-28
@Naminnuz

You just need to add an appropriate timeout to the test condition. For example, it can be done like this:

public class Checker {
    const double _stdDelta = 10.0d;
    Dictionary<string, DateTime> _dictionary = new Dictionary<string, DateTime>();

    bool check( string input, double? delta = null ) {
        if ( !_dictionary.ContainsKey( input ) ) {
            _dictionary.Add( input, DateTime.Now );
            return true;
        }

        var d = delta.HasValue ? delta.Value : _stdDelta;

        if ( ( DateTime.Now - _dictionary[input] ).TotalSeconds > d ) {
            _dictionary[input] = DateTime.Now;
            return true;
        }

        return false;
    }

    public void CheckConditionByIfs( int condition ) {
        if ( condition > 100 && check( "second_condition", 20.0d ) ) {
            Console.WriteLine( "Second_condition met, custom timeout set." );
        } else if ( condition > 20 && check( "first_condition" ) ) {
            Console.WriteLine( "First_condition met, standard timeout set." );
        } else {
            Console.WriteLine( "Nothing." );
        }
    }

    public void CheckConditionBySwitch( int condition ) {
        switch ( condition ) {
            case var _ when condition > 100 && check( "second_condition", 20.0d ):
                Console.WriteLine( "Second_condition met, custom timeout set." );
                return;
            case var _ when condition > 20 && check( "first_condition" ):
                Console.WriteLine( "First_condition met, standard timeout set." );
                return;
        }
        Console.WriteLine( "Nothing." );
    }
}

Usage example:
var rnd = new Random();
var values = new int[] { 10, 15, 20, 25, 30, 35, 40, 50, 60, 70, 80, 100, 150, 200 };
var c = new Checker();
int cnd;

while ( true ) {
    cnd = values[rnd.Next( values.Length )];
    Console.WriteLine( cnd );

    if ( rnd.Next( 100 ) > 50 )
        c.CheckConditionBySwitch( cnd );
    else
        c.CheckConditionByIfs( cnd );

    Thread.Sleep( 1500 );
}

Let me explain. C# 7.0 saw the birth of Pattern Matching, which you can observe in CheckConditionBySwitch (in terms of execution logic, it is no different from CheckConditionByIfs, so they are then randomly selected). By the release of C# 8.0, this will be quite a full-fledged option, as in Nemerle, Kotlin or F#, so you should take a closer look at switch and its new syntax now. In general, using Thread.Sleep for a timeout is bad form. It is inaccurate and gives something about when the system will have free resources. Much better to use some sort of TimeSpan thread as a timer. Anyway.
In this example, the conditions for different branches are separated by assigning them the current date-time by a unique string key (the key can contain, in principle, anything). After that, for acc. key, you check if it's time to update the value by key and return true, to enter the code branch. If you have uniquely limited options, then it would probably be worth creating an enum instead of string and checking / passing it already.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question