C# Console Application - Keep it running

前端 未结 11 1766
萌比男神i
萌比男神i 2020-12-16 01:02

I am about to develop a console application that will be required to continually run and carry out work at specific times.

My question is what is are best methods or

11条回答
  •  醉话见心
    2020-12-16 01:32

    Create a Task and then Wait it.

    class Program
    {
        static void Main(string[] args)
        {
            var processTask = Process();
            processTask.Wait();
        }
    
        private static async Task Process()
        {
            var isNotCancelled = true;
    
            while (isNotCancelled)
            {
                //Polling time here
                await Task.Delay(1000);
    
                //TODO: Do work here and listen for cancel
                Console.WriteLine("I did  some work...");
            }
        }
    }
    

提交回复
热议问题