.NET Framework Developer Center >
.NET Development Forums
>
.NET Base Class Library
>
WinForm application behaviour on Windows log off
WinForm application behaviour on Windows log off
- Hello.
I always thougth that if an application won't exit after a certain amount of time once logoff was initiated, then the users gets the "usual" [these apps prevents your system from restart kill/wait/back to windows] message.
But I've run some tests that shows that in most cases the app is killed (threads killed without executing a finally block) without that the end user had to decide anything...
First, I created an empty console app...
[STAThread] static void Main() { try { Application.ApplicationExit += new EventHandler(Application_ApplicationExit); while (true) { Thread.Sleep(1); } } finally { File.Create(@"c:\temp\Finally" + Thread.CurrentThread.ManagedThreadId.ToString()); } } static void Application_ApplicationExit(object sender, EventArgs e) { File.Create(@"c:\temp\Application_ApplicationExit" + Thread.CurrentThread.ManagedThreadId.ToString()); }
I compile the exe in release and starts it without debugging, then logs off: success, without any message to the user.
The endless main thread is killed, and I see no files created in c:\temp: the finally block isn't executed, and Application.ApplicationExit is not called...
I was really wondering... We see lots of apps like NotePad that prevents the system from logging off when a dialog box is opened... So I tried a stransard winform app:
public Form1() { InitializeComponent(); Application.ApplicationExit += Application_ApplicationExit; } void Application_ApplicationExit(object sender, EventArgs e) { File.Create(@"c:\temp\threadAppExit" + Thread.CurrentThread.ManagedThreadId.ToString()); } private void button1_Click(object sender, EventArgs e) { File.Create(@"c:\temp\threadGUI" + Thread.CurrentThread.ManagedThreadId.ToString()); MessageBox.Show(string.Empty, string.Empty, MessageBoxButtons.AbortRetryIgnore); } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { File.Create(@"c:\temp\threadFormClosing" + Thread.CurrentThread.ManagedThreadId.ToString()); MessageBox.Show("Form1_FormClosing"); } private void button2_Click(object sender, EventArgs e) { File.Create(@"c:\temp\threadGUI" + Thread.CurrentThread.ManagedThreadId.ToString()); while (true) { Thread.Sleep(1); } }
So... Once again, Application.ApplicationExit is never fired.
If I click button1, as you can see, a message box is displayed. When the user tries to log off in that state, then it fails! The user gets the windows message "kill or wait or return to windows". Good!
But something is weird anyway... I see from the file created by button1_Click in c:\temp that my GUI thread is THE SAME as the tread that calls Form1_FormClosing when the user tries to log off... Form1_FormClosing displays its own message box, and e.Cancel is not even set to true! But it works... The system knows (I don't know how) that he may not kill the GUI thread and the message loop without user action...
Then I do the button2 test... There, the GUI thread runs in an endless loop.
Log off... And once again, succes, no message to the user, message loop and gui threads (if they are not the same) are killed without warning...
So... I do not fell very confident with that behaviour. This worries me that my app threads may be killed (risk of data corruption) without warning to the user just because my GUI thread is busy for [a certain amount of time?]... And I really do not understand how the test with button1 worked, since my GUI thread was also busy, only just on a modal box instead of an endless loop...
Can anyone anyone help understanding that behaviour, and help me make sure my threads won't be killed without that the end user explicitely decides it? !
Thanks... ;)
Answers
- The answer is actually pretty simple: When you run the system message box, your thread is no longer running. The system is running the message box in its own 'thread'. When you attempt to log off, the OS pops up the message.
Obviously programs (like notepad) popup the system message box to ask things like "Are you sure you want to exit?", "Do you want to save your work?", etc.
If you want to know when your application is being terminated by logging off, use SystemEvents.SessionEnding.- Marked As Answer byeryangMSFT, ModeratorWednesday, November 11, 2009 8:47 AM
All Replies
- The answer is actually pretty simple: When you run the system message box, your thread is no longer running. The system is running the message box in its own 'thread'. When you attempt to log off, the OS pops up the message.
Obviously programs (like notepad) popup the system message box to ask things like "Are you sure you want to exit?", "Do you want to save your work?", etc.
If you want to know when your application is being terminated by logging off, use SystemEvents.SessionEnding.- Marked As Answer byeryangMSFT, ModeratorWednesday, November 11, 2009 8:47 AM


