C# Console Application - Keep it running

2019-01-23 15:56发布

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 practices to keep your application alive?

My thought were: A loop that never ends? A timer that sleeps and then jumps to routine when required (after set sleep period)?

I will be compiling the application into an exe and then running it as a service using AlwaysUp.

Regards..

Peter

11条回答
放荡不羁爱自由
2楼-- · 2019-01-23 16:01

Well I'm sure at some point it should stop, no?

Spawn a thread that does work, and have the main thread block on Console.ReadLine() if you want it to be runnable as a console app too.

If you really just want to pause the main thread forever, just block on a ManualResetEvent you never fire.

But, consider using a service if you can.

查看更多
\"骚年 ilove
3楼-- · 2019-01-23 16:01

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.

查看更多
冷血范
4楼-- · 2019-01-23 16:06

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).

查看更多
做个烂人
5楼-- · 2019-01-23 16:08

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...");
        }
    }
}
查看更多
欢心
6楼-- · 2019-01-23 16:09

Sending a thread to sleep: System.Threading.Thread.Sleep(10000);

Waiting for a key to be pressed: Console.WriteLine("Press any key to continue..."); Console.Read();

查看更多
聊天终结者
7楼-- · 2019-01-23 16:10

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.

查看更多
登录 后发表回答