0

I need a count down timer in C#. Can somebody please help me to create one. The requirement is:

Set Timer = 5 minutes;

for (i=0;i<100;i++)
{  
     RunfunctionA();

    if(TimeLeft==0)
        RunfunctionB();
}

Iterations keep on running; just that when timeleft==0, RunFunctionB() should be executed.

CSharpLearner
  • 21
  • 2
  • 4

1 Answers1

1

That sounds as if you just need a simple StopWatch:

var sw = new StopWatch();
var maxTime = TimeSpan.FromMinutes(5);
sw.Start();
for (int i=0; i < 100; i++)
{  
    RunfunctionA();

    if(sw.Elapsed <= maxTime)
    {
        RunfunctionB();
        break;
    }
}

If you want to countdown as commented, I would just calculate the remaining time:

TimeSpan remaining = maxTime - sw.Elapsed;

Edit: according to your comments you want to stop a process that is started in RunFunctionA if the time is elapsed, you could try this approach:

private Stopwatch ProcTimer = new Stopwatch();
private TimeSpan MaxTime = TimeSpan.FromMinutes(5);

private TimeSpan Remaining { get { return MaxTime - ProcTimer.Elapsed; } }

private void RunfunctionA()
{
    using (var proc = new System.Diagnostics.Process())
    {
        // initialization
        proc.Start();
        proc.WaitForExit((int)Remaining.TotalMilliseconds);
        if (proc.HasExited)
            ;// ...
        else
            proc.Kill();
    }
}

Here is the loop:

ProcTimer.Restart();
for (int i = 0; i < 100; i++)
{
    RunfunctionA();

    if (Remaining <= TimeSpan.Zero)
    {
        RunfunctionB();
        break;
    }
}
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • In your answer, the stopwatch is counting up or counting down? I need it to count down. How do we specify that? – CSharpLearner Sep 07 '13 at 22:38
  • No, it is counting up. However, it is just a simple calculation to get a countdown. Edited my answer. – Tim Schmelter Sep 07 '13 at 22:45
  • RunFunctionA() calls another process, which can take hours/minutes, I need to run RunFunctionB() as soon as timeleft=0, will this be taken care of, by your code above?. Please comment. Thanks – CSharpLearner Sep 07 '13 at 23:04
  • @CSharpLearner: No, because `RunfunctionA` will execute completely before the next statement gets evaluated at `if(sw.Elapsed...`. Then your pseudo code was not meaningful since it suggests that you just want to know at the `if` if the timespan has been elapsed. – Tim Schmelter Sep 07 '13 at 23:07
  • Sorry...but thats what I need...probably an asynchronous timer...that calls a function after some time is elapsed, independent of anything else..How should that be possible. – CSharpLearner Sep 07 '13 at 23:10
  • If you start a process in `RunFunctionA` you have to use [`Process.WaitForExit`](http://msdn.microsoft.com/en-us/library/ty0d8k56(v=vs.85).aspx). If it hasn't exited in the maximum time you have to use `Process.Kill`. – Tim Schmelter Sep 07 '13 at 23:11
  • To make my requirement clearer, I have this loop which runs a process(in RunFunctionA) for each iteration. Each process may take 'X' time. I want to let this process run, finish, run, finish...as many times in that loop...but when the total running time(of all iterations) becomes equal to MaxTime; any running process should be killed. I dont want to calculate timeelapsed for each iteration and get total time and compare ...because that has a limitation(i'll explain in next comment)....so,I instead want an independant timer...that has max duration and kills any running process after max time. – CSharpLearner Sep 07 '13 at 23:30
  • Limitation is: if (TotalTimeElapsedForIterationsRun< AllowedDuration) A process is called...but it may run for longer time than AllowedDuration – CSharpLearner Sep 07 '13 at 23:48
  • @CSharpLearner: I have edited my answer to show you an approach. It should give you a start. – Tim Schmelter Sep 08 '13 at 00:03
  • Tried a Timer approach and put a code above. Please have a look and let me know if this should be fine. Couldnt test as my code is in mess right now. – CSharpLearner Sep 08 '13 at 00:34