-1

I have a function enclosed in try catch that I would like to redo if there was an exception but time from start of the function is less than 1 hour. How to implement this, should I use some sort of loop?

public void myFunct()
{
    // start timer
    try
    {
        // do some work
    }
    catch (Exception ex)
    {
        // if timer less than 1 hour, go to start of try
    }
}
Lightsout
  • 3,454
  • 2
  • 36
  • 65

1 Answers1

0
public void myFunct()
{
    bool restart;
    var startTimer = new System.Diagnostics.Stopwatch();
    do
    {
        restart = false;
        startTimer.Start();
        try
        {
            // do some work
        }
        catch (Exception ex)
        {
            startTimer.Stop();
            if (startTimer.Elapsed.Hours < 1)
            {
                restart = true;
                startTimer.Reset();
            }
        }
    } while (restart);
}
Alexandru Lache
  • 477
  • 2
  • 14