Setting Timeouts?
I h开发者_开发百科ave a process that can get stuck in an infinite loop and I want to add a 5 second timeout so it doesn't hang forever.
bool FlagSuccess = false;
while (FlagSuccess == false)
{
try
{
//Blah blah blah
FlagSuccess=true;
}
catch
{
}
}
This is construct I try to avoid. But if you must:
bool FlagSuccess = false;
DateTime timeout = DateTime.UtcNow.AddSeconds(5);
while (FlagSuccess == false && DateTime.UtcNow < timeout)
{
try
{
//Blah blah blah
FlagSuccess=true;
}
catch
{
}
}
精彩评论