且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何不断在后台运行一个C#控制台应用程序

更新时间:2023-02-19 19:04:45

为什么不只是使用 Windows任务计划程序



将其设置为以所需时间间隔运行你的应用程序。它非常适合这类工作,你不必招惹强迫线程睡觉,可以创造更多的问题,它解决了。


I would like to know how to run a c# program in background every five minute increments. The code below is not what I would like to run as a background process but would like to find out the best possible method to do this using this code so that I can implement it on another code. so this process should run after a five minute increment. I know I could use threads to do so, but dont really now how to implement this. I know this is the best way How to run a console application on system Startup , without appearing it on display(background process)? this to run in the background, but how would I have the code running in five minute increments

 class Program
    {
        static void Main(string[] args)
        {
            Console.Write("hellow world");
            Console.ReadLine();
        }
    }

Why not just use Windows Task Scheduler?

Set it to run your app at the desired interval. It's perfect for this sort of job and you don't have to mess about with forcing threads to sleep which can create more problems that it solves.