10 Minutes Timer in C#

I'm developing a C# Console Application and I need to add a timer that starts when I write "start" and automatically stops when have passed 10 minutes. How can I do this using only the "static void Main()"?

I have this:

using System;
using System.Timers;

namespace myScript
{
    class Program
    {
        static void Main()
        {
            string getInput = Console.ReadLine();

            if (getInput == "start")
            {
                //start timer
            }

            if (//10 minutes have passed)
            {
                //do something
            }
        }
    }
}

Thank you!

3

2 Answers

//start timer
//put this into your if statement
Timer timer = new Timer (1000 * 60 * 10);
timer.Elapsed += delegate (object sender, EventArgs e) 
{
    //do something
    timer.Stop ();
    timer.Dispose ();
};
timer.Start ();

Try this, use system.timers not threading. This should start 10minute timer which does something and disposes itself at the end of operation.

0
    using System;
    using System.Threading;

static void Main(string[] args)
            {
                Console.WriteLine("Please type \"start\" and press ENTER");
                while (true)
                {
                    var userInput = Console.ReadLine();

                    if (userInput.Equals("start"))
                    {
                        break;
                    }
                    Console.WriteLine("Not correct, please try again");
                }


                var minutes = 10;
                Console.WriteLine("Going to sleep for " + minutes + " Minutes...");
                Thread.Sleep(1000 * minutes * 60);

                Console.WriteLine("Done...");

                Console.ReadLine();
            }
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Robert Thorne

Robert Thorne

Automotive & Future Transportation Editor

Robert Thorne covers electric vehicle innovations, autonomous driving systems, global mobility trends, and automotive engineering developments.

Share this article
Twitter Facebook Pinterest