Run and stop a method after a certain time by using Stopwatch. This can be done by using a stopwatch that is present in System.Diagnostics namespace. The below code is for reference.
using System;
using System.Diagnostics;
class Program
{
static void Main(string[] args)
{
ExecuteMethodWithTimer(60);
}
static void ExecuteMethodWithTimer(double maxRunTimeInSecs)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
while(true)
{
if (stopwatch.Elapsed > TimeSpan.FromSeconds(maxRunTimeInSecs))
{
break;
}
}
}
}
No comments:
Post a Comment