tags:

views:

47

answers:

2

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
    {
    }
}
+1  A: 

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
    {
    }
}
Joel Coehoorn
You need to change it to add 5 Seconds, not 5 minutes
w69rdy
I'm open to trying different things. What type of construct would you recommend I use? I'm very new to C#, and a lot of my code is just me fumbling in the dark. I'm happy to learn more "standards friendly" code practices while I'm still green.
Soo
I'd change that to `DateTime.UtcNow` otherwise you run the risk of either not spinning through the loop at all (really bad) or spinning through for more than an hour (slightly bad).
Brian Gideon
@Brian, can you explain to me how this loop running error could occur? If you're using DateTime.Now, I can only imagine it running into the error you describe if the timezone was changed while it was running...
Soo
@Soo: If you are in a region that observes daylight saving time then the spring transition jumps the time forward one hour and the fall transition jumps back one hour. I think you can visualize what happens from there. This is one among many reasons why it is always best to do **all** date/time processing in UTC. I have to deal with issues like these on daily basis that is why I am so padantic about it :) And yes, changing the timezone would also do it, but coding against that is on the verge of paranoia.
Brian Gideon
@Brian, Sorry if I'm being a jackass, I just want some clarification on the Now vs UTCNow issue. In the code example above (the one with the timeout), would a daylight savings time change only cause an hour/noLoop error if it occurred during those 5 seconds?
Soo
@Soo: Yes, that is correct. So if we have a 5s problem window that occurs twice in a year and if you assume that the code is started say every minute then you would have a 1 - (1 - 5/60)^2 = 0.16 = 16% probability of encountering the problem in a year. The odds drop significantly to only 0.3% if the code only runs every hour. Normally I would say weigh the risk versus the difficulty of the solution except in this case fixing the problem is so incredibly easy that you might as well use `UtcNow` and call it day.
Brian Gideon
@Brian, ok very good, thanks for that explanation. Knowing that there is a 16% chance per year of this error happening is enough for me to use UTCNow for this situation (and now I understand UTCNow well enough to use it in the future when warranted)<br/><br/>Thanks much! :D
Soo
A: 

Looks for the StackOverflow thread at http://stackoverflow.com/questions/299198/implement-c-generic-timeout Implement C# Generic Timeout

Hari Chinnan