Search code examples
c#delayasp.net-core-3.1

How to delay a certain task in c# .NET core 3.1


NOTE: I am a noob at coding.

I am trying to make a certain task in my application run after a certain amount of time one (for example) I want Console.WriteLine("Hello delay"); to run 180 seconds after Console.WriteLine("Hello World!"); is run, how would I do that?

I have not tried anything else yet.

using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            // I want (Console.WriteLine("Hello delay");) to run 
            // 180 seconds after (Console.WriteLine("Hello World!");) is run
            Console.WriteLine("Hello delay");
        }
    }
}

Solution

  • you can sleep current thread.

    using System;
    using System.Threading;
    
    namespace ConsoleApp1
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Hello World!");
                Thread.Sleep(180 * 1000);
                Console.WriteLine("Hello delay");
            }
        }
    }