简体   繁体   中英

Prevent console application from terminating

I have a C# 4.0 Windows Console application which should run 24*7*365. (I have a Windows Service to watch for this application, and start it if it terminates for some reason). The startup of this Console application involves some complex resource consuming activities - so I have to make sure that no frequent restarts of the console application happens. The console application has 3 System.Timers.Timer instances which do some activities when the timers are elapsed (intervals are 10 seconds, 15 seconds and 2 hours resp). What would be the correct method to prevent the console application from terminating, and keep it alive? Could someone please suggest!

From what you have said in your comments the only reason you have a console application is so you can read debugging messages. This answer will go off that assumption.

A console app is not reliable enough to do what you want to do, there are many situations that you will encounter that the app will not launch or may launch multiple times (what do you do if no one is logged in? What do you do if multiple people are logged in?)

The best thing to do is write this intensive operation as a service, then whatever diagnostic information you where outputting to the console you can either put it to the event log or put it to the Debugger.

If you want to write a message to the Application event log all you need to do is write your information to this.EventLog.WriteEntry(message) . This will generate a Information event in the Application Event Log for your service. If you want to raise the severity to Warning or Error, you just need to use the other overload .

If you want to write to a different log than Application (like your own log) you will need to have the installer of the service create whatever sources/logs you need (ask a new question if you need help with this). Then set AutoLog in your service to false then create your own event log instance pointing at your new log.

If you want logs that only show up when you are diagnosing a problem the simplest solution is to change your Console.WriteLine(message) statements to Debugger.Log(1, "DebugLogging", message + Environment.NewLine) . This will cause your messages to show up in the "Debug" output window when you are attached to the service while it is running (Do you know how to attach to a service to run your debugger on it? If not ask a new question on it). I would not recommend using Debug.WriteLine(message) unless you never plan on doing diagnostics while built in release mode. This is because Debug.WriteLine has the attribute [Conditional("DEBUG")] . What this does is cause the the function only to be executed if the DEBUG flag is set during compile time, and Release does not have that flag set.


That is the simple solution, if you want a more robust solution I recommend learning how to use log4net . Log4net will let you do all of the above, plus a lot more. For example you can control how much gets written to the event logs via a configuration file (The program does not even need to be restarted to start logging with the new settings, it detects when you perform a save.)

This way you can put verbose logging messages in your code but only turn them on when you need them by changing one setting in a XML config file.

If you have any questions about setting up either of the two top suggestions or setting up log4net feel free to start new questions on those topics.

You could simply use a Console.ReadLine() to keep the console alive. But a Windows service is probably what you want.

while (Console.ReadLine() != "I really want to stop this application") { } or something similar?

Thinking of similar applications (eg Minecraft...) you'd have a loop such as the following:

string command;
Console.Write("> ");
while ((command = Console.ReadLine()).ToUpper() != "STOP")
{
    // Do something else based on the command
    ...

    Console.Write("> ");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM