C# Console Application - Keep it running

前端 未结 11 1713
萌比男神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:44

    If your program is going to be continually running then you should sleep until the desired event occurs (e.g. XX seconds passes). If you just spin in a while {} loop you'll suck up CPU times.

    If your program is going to always be running on a machine then you should consider making it a service so it automatically starts and stops with the machine.

    0 讨论(0)
  • 2020-12-16 01:45

    Code that runs continually is called a daemon and there is an article here outlining how to do what you ask. That will point you to an example of how to write a simple service here.

    0 讨论(0)
  • 2020-12-16 01:52

    You can add a reference to System.Windows.Forms and call System.Windows.Forms.Application.Run() to begin a standard application message loop. Ctrl-C will terminate the app.

    Another option is to use Console.ReadKey() to pause the app. Like Console.WriteLine("Press [ANY] key to quit..."); Console.ReadKey();

    That's what I use in my console apps when they're just sitting there waiting for events to occur. In either case the app will continue to run and catch triggered events (like from a timer, WCF, FileWatcher, etc).

    0 讨论(0)
  • 2020-12-16 01:54

    If your building a desktop application you'll want to have it run in the system tray. This will

    1. Keep your users from accidentally closing the application
    2. Keep your application from cluttering the screen of your users

    If your building a server application you will want to write a windows service. This will

    1. Keep an administrator from accidentally closing your application
    2. Eliminate the need for the server to have someone logged into the console for your application to be running in

    As someone who is primarily an IT Pro I would say that 3rd party applications that we get that run as console apps instead of windows services we put a lot of effort into keeping from being purchased. It creates a lot of work for us, and opens up significant support issues and security holes.

    0 讨论(0)
  • 2020-12-16 01:57

    Why don't you build your app as a service in the first place?

    0 讨论(0)
提交回复
热议问题