0

I have a C# console application. I want to execute a function every minute, for up to 1 hour. The function returns a bool value. If the function returns true, the timer should stop else it should keep executing every minute, for up to 1 hour. Below is my code I have written so far.

static void Main(string[] args)
{
    var timer = new System.Timers.Timer(60000);
    timer.Elapsed += new 
    System.Timers.ElapsedEventHandler(CheckEndProcessStatus);
    //if timer timed out, stop executing the function
}

private void CheckEndProcessStatus(object source, ElapsedEventArgs args)
{
    try
    {
        if (CheckFunction())
        {
            //stop timer
        }
    }
    catch(Exception ex)
    {
        throw;
    }
}

private bool CheckFunction()
{
    bool check = false;

    try
    {
        //some logic to set 'check' to true or false
    }
    catch (Exception ex)
    {
        throw;
    }
    return check;
}

I think I need some guidance on this to implement this. Please let me know if I can provide more details on this.

Souvik Ghosh
  • 4,456
  • 13
  • 56
  • 78
  • 1
    *The code is not complete* What make you think so? what you need to implement – sujith karivelil May 31 '17 at 05:43
  • If it's just this simple application you could try using a `while (true)` and then `Thread.Sleep(60000)` (1 minute) instead of timers, then you could simply break out of the while loop by returning a `false` boolean. However, that would lock up the main thread so I wouldn't suggest using it if the project is bigger than what you've shown here. More info: https://stackoverflow.com/questions/8815895/why-is-thread-sleep-so-harmful – Christopher Lake May 31 '17 at 06:38
  • @ChristopherLake Yes I am currently doing that. But that's not a good way to do this as you know already. – Souvik Ghosh May 31 '17 at 07:01
  • Yeah I can see, I was just offering an alternative to timers. But your code looks correct so I'm not quite sure what you're asking for then? – Christopher Lake May 31 '17 at 09:34

1 Answers1

2

Just call timer.stop() to stop the timer. It internally calls timer.Enabled = false Use another timer to stop the first timer after one hour.

    private static Timer timer;
    private static Timer oneHourTimer;

    static void Main(string[] args)
    {
        oneHourTimer = new System.Timers.Timer(3600 * 1000);
        timer = new System.Timers.Timer(60 * 1000);

        timer.Elapsed += new System.Timers.ElapsedEventHandler(CheckEndProcessStatus);
        oneHourTimer.Elapsed += oneHourTimer_Elapsed;

        oneHourTimer.Start();
        timer.Start();
    }

    static void oneHourTimer_Elapsed(object sender, ElapsedEventArgs e)
    {
        timer.Stop();
        //maybe stop one hour timer as well here
        oneHourTimer.Stop();
    }

    private static void CheckEndProcessStatus(object source, ElapsedEventArgs args)
    {
        try
        {
            if (CheckFunction())
            {
                //stop timer
                timer.Stop();
            }
        }
        catch (Exception ex)
        {
            throw;
        }
    }

    private static bool CheckFunction()
    {
        bool check = false;

        try
        {
            //some logic to set 'check' to true or false
        }
        catch (Exception ex)
        {
            throw;
        }
        return check;
    }
Alok Jha
  • 353
  • 3
  • 9