开发者

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
    {
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜