How do I expect a bool to be true?

1

I have this async method that currently expects a time declared by me. The program first restarts the boolean of result and then sends commands to a serial that after a certain time returns in my program the results in that same boolean .

But it's obviously a bad practice to have to set the time " hard coded " because for some reason the response may be faster than expected, or slower. Here is a sample of what the code looks like:

public async Task StartNewTest(TestType test)
{
   double time = 0;
   switch(test)
   {
     case TestType.Alerts:
        time = 20000;
        AlertOK = false;
        SendSerial("A");
     case TestType.Lights:
        time = 18000;
        LightsOK = false;
        SendSerial("L");
   }
   await Task.Delay(TimeSpan.FromMilliseconds(time));
}

In this way, it is functional. But I would like to stop using this form, this time, expecting the% boolean AlertOK to be true for example.

As if it were (just an idea). await AlertOK == true

    
asked by anonymous 15.06.2018 / 19:25

1 answer

2

I do not know how the method that calls your StartNewTest is implemented, however you can start this method in another thread :

await Task.Factory.StartNew(async () =>
{           
    await StartNewText(TestType);
});

This allows us to use a while without locking UI :

case TestType.Lights:
{
    LightsOK = false;
    SendSerial("L");
    while(AlertOK == false) {}
    break;
}

In this way you can remove await Task.Delay(TimeSpan.FromMilliseconds(time)) and your method will stay in there until the AlertOK variable becomes true .

    
15.06.2018 / 20:28