Wednesday, February 5, 2020

Run and stop a method after certain interval

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

Labels

How to take screenshot of a Webpage using C# Selenium

           ChromeOptions options = new ChromeOptions();             options.AddArgument("headless");//Comment if we want to see ...