How to Guarantee Fast Shutdown of My Win32 App

How to exit SW quickly

The fastest way to terminate a process is to call TerminateProcess(). This simply stop the process from receiving any further time slices and releases all the resources allocated to it by windows.

This method of ending a process is not recommended though for various reasons. One of the for instance is that if you have an open file which you write to using the standard library, some data may still be buffered and not fully written to the file.

Efficient exit from multithreaded application (specifics)

In Windows, I use QueueUserAPC to call a function which throws an exception, causing threads to exit cleanly.

I wrote more about the details in this answer here:

How do I guarantee fast shutdown of my win32 app?

In summary, here's what happens:

Say thread A wants to terminate thread B (and then C, D, ...)

  • Thread A calls QueueUserAPC(), passing the handle to thread B and the address of a function which will throw an Exception of class MyThreadExit.
  • Thread B runs normally until it calls something that checks for alertable waits. Maybe WaitForSingleObjectEx, maybe SleepEx, or something else.
  • At this point, thread B runs the APC function passed earlier, causing the exception to be thrown in Thread B.
  • All stack-allocated objects get automatically destructed correctly as the exception makes thread B 'unwind' its stack.
  • The outermost thread function of thread B will catch the exception.
  • Thread B now exits, possibly signalling to Thread A that it's done.

How do I shutdown, restart, or log off Windows via a bat file?

The most common ways to use the shutdown command are:

  • shutdown -s — Shuts down.
  • shutdown -r — Restarts.
  • shutdown -l — Logs off.
  • shutdown -h — Hibernates.

    Note: There is a common pitfall wherein users think -h means "help" (which it does for every other command-line program... except shutdown.exe, where it means "hibernate"). They then run shutdown -h and accidentally turn off their computers. Watch out for that.

  • shutdown -i — "Interactive mode". Instead of performing an action, it displays a GUI dialog.

  • shutdown -a — Aborts a previous shutdown command.

The commands above can be combined with these additional options:

  • -f — Forces programs to exit. Prevents the shutdown process from getting stuck.
  • -t <seconds> — Sets the time until shutdown. Use -t 0 to shutdown immediately.
  • -c <message> — Adds a shutdown message. The message will end up in the Event Log.
  • -y — Forces a "yes" answer to all shutdown queries.

    Note: This option is not documented in any official documentation. It was discovered by these StackOverflow users.


I want to make sure some other really good answers are also mentioned along with this one. Here they are in no particular order.

  • The -f option from JosephStyons
  • Using rundll32 from VonC
  • The Run box from Dean
  • Remote shutdown from Kip

Gently kill a process

Kernel event objects come to mind: your "manager" raises a named event. Your child processes should check the state of this event at least once in a while (or have a thread that continuously checks it).



Related Topics



Leave a reply



Submit