user773737
user773737

Reputation:

C# application closing after a week or two

Windows server 2008 R2

I have a C# console application that is meant to run indefinitely.

So I put Console.readLine() in it to try and keep it up.

However, perhaps a week later, I'll come back to it and it'll have closed, as if someone had pressed the X button and closed the window.

If it had crashed, I'd get a stack trace and a "This Program has closed unexpectedly" like you get when any other program crashes, and then it'd wait for user input, but I don't even see that.

I just come back a week or two later, and the console window isn't up anymore.

Why is this?

I start the application by double-clicking the .exe on the desktop.

Upvotes: 2

Views: 103

Answers (2)

Marcel N.
Marcel N.

Reputation: 13976

If this app does work periodically, in tighter intervals, then a Windows Service is a better way to do it. Also, a Windows Service can't be closed with an Enter key press.

If it needs to do work a few times a day or similar, maybe you can consider starting the app at regular intervals via Scheduled Tasks.

In any case, keeping a console application open like this is subject to more reasons for closing "unexpectedly". For example, besides system restarts the user might also log off.

On your current solution you could detect what's happening by adding a log file:

  1. Log all exceptions (including app domain unhandled exceptions).
  2. Log when the enter key is pressed, so right after the ReadLine. You'll be able to tell for sure what's happening.
  3. Hook up to the SystemEvents.SessionEnding events and log this as well (for log off, reboot, etc).

You can integrate a logging framework easy enough with log4net, for example.

Upvotes: 3

czuroski
czuroski

Reputation: 4332

As discussed in the comments, please confirm that the machine does not have automatic updates enabled as they can interfere with applications running.

Upvotes: 0

Related Questions