I’ve found myself coding this type of thing a few times.
for (int i = 0; i < 10; i++)
{
if (Thing.WaitingFor())
{
break;
}
Thread.Sleep(sleep_time);
}
if(!Thing.WaitingFor())
{
throw new ItDidntHappenException();
}
It just looks like bad code, is there a better way of doing this / is it a symptom of bad design?
A much better way to implement this pattern is to have your
Thingobject expose an event on which the consumer can wait. For example aManualResetEventorAutoResetEvent. This greatly simplifies your consumer code to be the followingThe code on the
Thingside is also not really any more complex.