0

I'm using a ConsoleApplication for a game server. I know this question has been asked before, but I'm looking for a work around. The server initializes all the data and then starts the acceptor manager which waits for clients. After this, it's basically done. Everything is running in the background so it just exists. So far I've used:

while (true)
    Console.Read();

However, sometimes when an exception is thrown I want to shut down the server and show a message: Press any key to quit..., but that's not possible because of the loop.

Is there a workaround for this?

M4N
  • 94,805
  • 45
  • 217
  • 260
Gilbert Williams
  • 970
  • 2
  • 10
  • 24
  • Any non-trivial .NET program should have an event handler for the AppDomain.CurrentDomain.UnhandledException event. You can display your "Press any key" message in that event handler. And call Environment.Exit() to terminate the program. – Hans Passant Jul 29 '15 at 16:16

1 Answers1

0

You can use an event loop, like in the question Making a console application like a Windows application.

Once you have the event loop, you can attach a global exception handler that will shut down your application as intended in the event of an uncaught exception on the other threads.

Community
  • 1
  • 1
Jacob Krall
  • 28,341
  • 6
  • 66
  • 76