ner egsegd
ner egsegd

Reputation: 57

How to check if specific time has passed

I am start my operation and one of the argument my command line application get is Number that represent how much time my operation need to run.

int duration = int.Parse(args[0]) // run my operation for this time in minutes

This is what i have try (not working):

...
DateTime start = DateTime.Now;
// check whether this time in minutes passed
if (start.Minute > duration )
    break;

Upvotes: 2

Views: 14902

Answers (2)

husonos
husonos

Reputation: 241

For this you can use StopWatch by defining

StopWatch watch = new StopWatch();
watch.Start();

and where ever ur code finishes running write

watch.Stop();

By using stopwach you can see you are running time of your application in detail. Of course if I am correct to understand you.

Upvotes: 3

ShayD
ShayD

Reputation: 930

EDIT: use UtcNow

    void DoWork(int durationInMinutes)
    {
        DateTime startTime = DateTime.UtcNow;
        TimeSpan breakDuration = TimeSpan.FromMinutes(durationInMinutes);

        // option 1
        while (DateTime.UtcNow - startTime < breakDuration)
        {
            // do some work
        }

        // option 2
        while (true)
        {
            // do some work
            if (DateTime.UtcNow - startTime > breakDuration)
                break;
        }
    }

Upvotes: 9

Related Questions