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