peter
peter

Reputation: 8682

conditional check to wait for favourable condition

i have a condtion if (!e.ComponentUp) ,if its true then i need to wait until it Up what check i can give here

I did this code but its not working

if (!e.ComponentUp)
{
do 
{ 
 while (e.ComponentUp);
}
}

Upvotes: 0

Views: 562

Answers (3)

cordialgerm
cordialgerm

Reputation: 8503

You could create an event on your Component called something like 'UpChanged' and subscribe to the event. When you come to the code that can only run when the component is in the 'Up' state you could set a flag that indicates your 'task' is waiting for the up state to change. Something like this:

//When you initialize
var e = ...;
e.UpChanged += new EventHandler(Component_UpChanged);
bool waitingForUp = false;

//The code snippet that is waiting for up to be enabled
if (!e.ComponentUp)
     waitingForUp = true;
else 
     DoWorkHere();

//The delegate
void Component_UpChanged(object sender, EventArgs e)
{
     if (e.ComponentUp && waitingForUp)
          DoWorkHere();
     waitingForUp = false;

}

Upvotes: 0

Steven Sudit
Steven Sudit

Reputation: 19640

Your code cannot work.

First, if the predicate is false, then the while loop will exit immediately. You would need to loop on the negation.

Second, unless it's volatile and being changed asynchronously, the loop won't end.

Third, this sort of polling will max out the CPU.

Use an event.

Upvotes: 1

thelost
thelost

Reputation: 6704

Why don't you use events ? Is that a 3rd party component ?

Upvotes: 0

Related Questions