0

I want to check the string value for a specified time. I set it. During this time you will press the button to change the value of the string and achieve the condition. If you did not press the button during the specified time you will not check the condition. If the time is over, the condition will not be met . When using this code, the condition is always met :( :

string value;
private void button3_Click(object sender, EventArgs e)
{
    IAsyncResult result;
     Action action  = () => 
     {
         do //loop to check value through 10s
         {
            return;
         }
         while ( value == "");              
     };
     result = action.BeginInvoke(null, null);
     if(result.AsyncWaitHandle.WaitOne(10000)) 
                  //wait 10s to check response
    {
         listBox2.Items.Add("good"); // if response string value != ""
    }
    else 
    {
         listBox2.Items.Add("bad"); 
              // if response string value == "" or timeout  
    }
}

private void button4_Click(object sender, EventArgs e)
{
    value = "best"; // add value
}
  • 1
    `do { return; } while(value == "");` This loop doesn't do what you think it does. it is going to instantly return from that function (i.e. the action) and never even check if `value == ""`. And if that's what you expect it to do, why not just call `return;` and not have the loop at all. – Shelby115 Nov 01 '18 at 13:10
  • Thank you . Because I do not know when the user will put value. So I want a loop to check the value over a specified period – Nour Nesaan Nov 01 '18 at 13:15
  • Why don't you use [some kind of observable](https://stackoverflow.com/a/1249534/1462295) or hook the change event of the user input? – BurnsBA Nov 01 '18 at 13:28
  • I do not want to use this type. I want to use a loop or something like it – Nour Nesaan Nov 01 '18 at 13:42

1 Answers1

0

This would be my approach to your problem, that do while loop, wont do any good honestly

    string value;
    private void button3_Click(object sender, EventArgs e)
    {
        WaitSecondsAndExecute(10);

    }

    private async void WaitSecondsAndExecute(int seconds)
    {
        for (int i = 0; i < seconds; i++)
        {
            await Task.Delay(1000);
            if (value != null)
            {
                break;
            }
        }

        if (value != null)
        {
            listBox2.Items.Add("good"); // if response string value != ""
        }
        else
        {
            listBox2.Items.Add("bad");
            // if response string value == "" or timeout  
        }
    }

    private void button4_Click(object sender, EventArgs e)
    {
        value = "best"; // add value
    }

if you need it faster its a matter of dividing the delay and multiplying the seconds for the same amount

nalnpir
  • 1,106
  • 4
  • 14
  • Thank you . But the problem is if the value is set in the first second, it waits for the whole time. – Nour Nesaan Nov 01 '18 at 13:24
  • @NourNesaan Thats not hard to solve at all, i thought u wanted to do it when it finished the time i ll edit the answer – nalnpir Nov 01 '18 at 16:48