sooprise
sooprise

Reputation: 23187

Setting Timeouts?

I have 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
    {
    }
}

Upvotes: 1

Views: 423

Answers (1)

Joel Coehoorn
Joel Coehoorn

Reputation: 416121

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

Upvotes: 2

Related Questions