Answered by:
Application's close event and Windows logout

Question
-
It doesn't seem like a C# application's close event is processed on Windows logout. I.e. As a test, i've placed a StreamWriter in an aplication's close event to write a single line of text, but the file is never written to disc.
Is there another way to guarantee the close event is processed before windows logout?Monday, November 3, 2008 4:02 PM
Answers
-
try FormClosing event.
if you found answer for your question , always click the 'Mark as Answer' button on the respective answers.- Proposed as answer by Yam Sapkota Wednesday, November 5, 2008 8:31 PM
- Marked as answer by Michael Sun [MSFT]Microsoft employee Thursday, November 6, 2008 6:46 AM
Monday, November 3, 2008 4:12 PM -
Simple Example, hope this is what you are looking for
using Microsoft.Win32;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
SystemEvents.SessionEnded += new SessionEndedEventHandler(SystemEvents_SessionEnded);
}
public void SystemEvents_SessionEnded(object sender, SessionEndedEventArgs e)
{
MessageBox.Show("I am Working");
}
}
}
Edit: Please make sure you use close event to write log as well since user closes your application manually too
Arjun Paudel- Edited by Arjun Paudel Monday, November 3, 2008 4:20 PM
- Marked as answer by Michael Sun [MSFT]Microsoft employee Thursday, November 6, 2008 6:46 AM
Monday, November 3, 2008 4:17 PM
All replies
-
try FormClosing event.
if you found answer for your question , always click the 'Mark as Answer' button on the respective answers.- Proposed as answer by Yam Sapkota Wednesday, November 5, 2008 8:31 PM
- Marked as answer by Michael Sun [MSFT]Microsoft employee Thursday, November 6, 2008 6:46 AM
Monday, November 3, 2008 4:12 PM -
Simple Example, hope this is what you are looking for
using Microsoft.Win32;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
SystemEvents.SessionEnded += new SessionEndedEventHandler(SystemEvents_SessionEnded);
}
public void SystemEvents_SessionEnded(object sender, SessionEndedEventArgs e)
{
MessageBox.Show("I am Working");
}
}
}
Edit: Please make sure you use close event to write log as well since user closes your application manually too
Arjun Paudel- Edited by Arjun Paudel Monday, November 3, 2008 4:20 PM
- Marked as answer by Michael Sun [MSFT]Microsoft employee Thursday, November 6, 2008 6:46 AM
Monday, November 3, 2008 4:17 PM