Reputation: 99
I am trying to implement something that waits for a boolean to be true. If after 5 seconds the boolean is still not true then i will execute the error message code
This is what I am doing now. But this method just waits for 5 seconds for all cases, which is wasting time. How can I do something like that this that executes as soon as the variable becomes true?
Thread.Sleep(5000);
if (imageDisplayed == true) {
//success
}
else {
//failed
}
Upvotes: 0
Views: 6195
Reputation: 37060
You can set a timeout variable to the time that you want to stop waiting and use that, along with the check that you're waiting for, as a condition in a while loop. In the example below, we just sleep for a tenth of a second between checks, but you can adjust the sleep time (or remove it) as you see fit:
var timeout = DateTime.Now.AddSeconds(5);
while (!imageDisplayed && DateTime.Now < timeout)
{
Thread.Sleep(100);
}
// Here, either the imageDisplayed bool has been set to true, or we've waited 5 seconds
Upvotes: 1
Reputation: 5953
Better to use a ManualResetEvent for this.
// Somewhere instantiate this as an accessible variable to both
// display logic and waiting logic.
ManualResetEvent resetEvent = new ManualResetEvent(false);
// In your thread where you want to wait for max 5 secs
if(resetEvent.WaitOne(5000)) { // this will wait for max 5 secs before continuing.
// do your thing
} else {
// run your else logic.
}
// in your thread where you set a boolean to true
public void DisplayImage() {
// display image
display();
// Notify the threads waiting for this to happen
resetEvent.Set(); // This will release the wait/lock above, even when waiting.
}
Rule of thumb. Better not use sleeps in your production code unless you have a really, really, really good reason to do so.
Upvotes: 4
Reputation: 1
Sounds like you want to use the System.Timers.Timer class.
Setup your boolean variable to execute a function when it is set to true
System.Timers.Timer t;
private bool val;
public bool Val {
get { return val; }
set
{
if (value == true)
// run function here
val = value;
}
}
Then Setup your timers interval for every 5 seconds.
public Main()
{
InitializeComponent();
t = new System.Timers.Timer(5000);
t.Elapsed += T_Elapsed;
}
private void T_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
throw new Exception();
}
To start the timer simply use t.Start()
and t.Reset()
to reset the timer
Upvotes: -2
Reputation: 3020
Use a while loop and checking incrementally for your condition
var waitedSoFar = 0;
var imageDisplayed = CheckIfImageIsDisplayed(); //this function is where you check the condition
while(waitedSoFar < 5000)
{
imageDisplayed = CheckIfImageIsDisplayed();
if(imageDisplayed)
{
//success here
break;
}
waitedSoFar += 100;
Thread.Sleep(100);
}
if(!imageDisplayed)
{
//failed, do something here about that.
}
Upvotes: 0
Reputation: 7409
Break your sleeps up into "naps." ;)
for (int n = 0; n < 50; n++)
{
Thread.Sleep(100);
if (imageDisplayed)
{
// success
break;
}
}
//failed
Not quite instantly, but with a maximum 100ms latency.
Upvotes: 0