C# .Net exe doesn't close when PC is restarted, keeping the machine from restarting
Asked Answered
B

6

5

We have a SmartClient built in C# that stubornly remains open when the PC its running on is being restarted. This halts the restart process unless the user first closes the SmartClient or there is some other manual intervention.

This is causing problems when the infrastructure team remotely installs new software that requires a machine reboot.

Any ideas for getting the SmartClient app to recognize the shutdown/restart event from Windows and gracefully kill itself?

UPDATE: This is a highly threaded application with multiple gui threads. yes, multiple gui threads. Its really a consolidation of many project that in and of themselves could be standalone applications - all of which are launched and managed from a single exe that centralizes those management methods and keeps track of those threads. I don't believe using background threads is an option.

Bow answered 3/10, 2008 at 18:47 Comment(2)
Are you running stuff on non-background threads?Freesia
Do you have access to the source code for the SmartClient app?Grundy
G
5

OK, if you have access to the app, you can handle the SessionEnded event.

...
Microsoft.Win32.SystemEvents.SessionEnded +=new
  Microsoft.Win32.SessionEndedEventHandler(shutdownHandler);

...

private void shutdownHandler(object sender, Microsoft.Win32.SessionEndedEventArgs e) {
  // Do stuff
}
Grundy answered 3/10, 2008 at 18:58 Comment(1)
Just a note: You probably want the SessionEndING event, not the SessionEnded (if you need to abort threads and close forms etc).Feld
P
5

It must be a thread that continues to run preventing your application to close. If you are using threading an easy fix would be to set it to background.

A thread is either a background thread or a foreground thread. Background threads are identical to foreground threads, except that background threads do not prevent a process from terminating. Once all foreground threads belonging to a process have terminated, the common language runtime ends the process. Any remaining background threads are stopped and do not complete.

http://msdn.microsoft.com/en-us/library/system.threading.thread.isbackground.aspx

Plastometer answered 3/10, 2008 at 18:55 Comment(1)
Indeed, this is a threaded app. See update to original post. However, given what we're doing with the threads, is backgrounding them still viable? Ie, multiple GUI threads.Bow
G
5

OK, if you have access to the app, you can handle the SessionEnded event.

...
Microsoft.Win32.SystemEvents.SessionEnded +=new
  Microsoft.Win32.SessionEndedEventHandler(shutdownHandler);

...

private void shutdownHandler(object sender, Microsoft.Win32.SessionEndedEventArgs e) {
  // Do stuff
}
Grundy answered 3/10, 2008 at 18:58 Comment(1)
Just a note: You probably want the SessionEndING event, not the SessionEnded (if you need to abort threads and close forms etc).Feld
D
5

When a user is logging off or Windows is being shut down, WM_QUERYENDSESSION message is sent to all top-level windows. See MSDN documentation here.

The default behavior of a WinForm application in response to this message is to trigger the FormClosing event with CloseReason == WindowsShutDown or others. The event handler though can choose to be stubborn and refuse to shut the app down, thus keeping the system running.

Check FormClosing handlers of your applications. Maybe there is something in there. I've seen this kind of stuff a couple of times.

Drawers answered 3/10, 2008 at 19:27 Comment(0)
P
1

Or maybe the .Net app is ignoring close or quit messages on purpose?

Psychoanalysis answered 3/10, 2008 at 18:52 Comment(0)
P
1

Background threads was a quick and dirty solution, best solution is to use synchronization objects (ManualResetEvent, Mutex or something else) to stop the other threads;

Or else keep track of all your opened windows and sent WM_CLOSE message when main app closes.

You have to give more information about how do you start those GUI applications. maybe you start one thread for each application and call Application.Run(new Form1()); ?

You may also look into creating a AppDomain for each GUI Application

Plastometer answered 3/10, 2008 at 19:9 Comment(0)
I
0

Normally a .Net app would respond correctly- at least, that's the 'out of the box' behavior. If it's not, there could be a number of things going on. My best guess without knowing anything more about your program is that you have a long-running process going in the main UI thread that's preventing the app from responding to window messages.

Ivaivah answered 3/10, 2008 at 18:49 Comment(1)
Not long-running processes but do have Threading as Catalin mentioned above. Still, that doesn't really answer the question - I'm really looking for a way to detect when the machine is being restarted.Bow

© 2022 - 2024 — McMap. All rights reserved.