SessionEnding event (C#)

Eran

Member
Joined
Apr 12, 2003
Messages
69
Hi,

I looked in all forums. MSDN doesnt have a relevant sample for that.
I have found in some forum, a code which was added by a guy, who swored on his mama that it works. :)

NADA. it didnt.

Any one knows how to impliment this event?
 
The code probably does work but the problem I found was that the close event was called before the SessionEnding event. So the SessionEnding event was never called because the application was terminated.

His code probably did work. The question is, how do you make sure SessionEnding is called before closing.

See this post:
http://www.computerhelp.forum/t71780-1.html

I had the same problem.
 
It looks like microsoft could have add some more info on these events. I find that the .NET MSDN has a lack of information.
 
Youre right about that. And their code on should be a different font and color coded like the program does. Thats my opinion.
 
This is my code. In some re3ason, the file is not been created. also messagebox cant added to the "CloseEvent" function.
I also tried to use the "SessionEnding" event and the same results. Ill be thankfull if you help me.

C#:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Microsoft.Win32;
using System.IO;
using System.Text;

static void Main() 
{
    Form1 myfrm = new Form1();
    SystemEvents.SessionEnded  += new ssionEndedEventHandler(myfrm.CloseEvent);
    Application.Run(new Form1());

}

public void CloseEvent(object sender,SessionEndedEventArgs  e)
{
    FileStream fs = null;
    string path;
    byte[] msg ;
    path = @"c:\Eran\Attendance reports\Report.txt";
    fs = new FileStream(path, FileMode.Create);
    msg = Encoding.ASCII.GetBytes(e.Reason.ToString());
    fs.Write(msg,0,msg.Length);
    if (fs != null) 
    {
        fs.Flush();
        fs.Close();
    }					
}
 
Last edited by a moderator:
First of all, I dont think this is correct:
SystemEvents.SessionEnded += new ssionEndedEventHandler(myfrm.CloseEvent);

ssionEndedEventHandler should be SessionEndedEventHandler.

Most important:
The SessionEnded event handler is for when the user is shutting down the computer, not closing the program. I think you are looking for the Closing event to do some finishing up before the program closes.

this.Closing += new System.ComponentModel.CancelEventHandler(this.Form1_Closing);

private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{

}
 
I was looking for the shutting down the computer. I think you were right, I did a mistake in the "session...".After some ocde changing, it worked. But, I added the command
C#:
e.cancel = true;
and in the end of the code I added
C#:
e.cancel = false;
I hope it didnt make the difference.
 
read the link that aewarnick posted above

i replied


and this session thing yes, MSDN has taken note that frm closing and session ending cannot be determined of who goes first so the thin they have said is to override ur wnprc
 
funny, but some how the link that aewarnick posted has changed to a link about: "Setting a Form to Be Invisible at Its Inception"
which I needed it also. maybe he knew what will be my next question"
 
Weird, It seems that the SessionEnding event when implemented in Console application doesnt behave as it should be. I get a message from my PC that the program is not responding.
 
You dont need e.Cancel=fasle at the end of your code.
This is what you are looking for:

C#:
StackTrace Trace = new StackTrace(true);
if(Trace.FrameCount > 13)
{
        if(Trace.GetFrame(14).GetMethod().Name=="WmSysCommand")
	{
                //The user is closing the form.
		e.Cancel=true;
		this.Visible=false;
	}
}
Put that in your CLOSING event. Dont use SessionEnding.
 
Last edited by a moderator:
This might help for Session Ending Event

SystemEvents.SessionEnding Event

Occurs when the user is trying to log off or shutdown the system

This is a cancellable event. Setting the Cancel property to false will request that the session continues to run. It provides no guarantee that the session will not end.
If you are using SessionEnding in a Windows form to detect a system logoff or reboot, there is no deterministic way to decide whether the System.Windows.Forms.Form.Closing event will fire before this event.
If you want to perform some special tasks before System.Windows.Forms.Form.Closing is fired, you need to ensure that SessionEnding fires before System.Windows.Forms.Form.Closing. To do this, you need to trap the WM_QUERYENDSESSION in the form by overriding the WndProc function. The following example demonstrates how to do this in a deterministic way,

[Visual Basic Code]

Code:
Private Shared WM_QUERYENDSESSION As Integer = &H11
Private Shared systemShutdown As Boolean = False
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
    If m.Msg = WM_QUERYENDSESSION Then
        MessageBox.Show("queryendsession: this is a logoff, shutdown, or reboot")
        systemShutdown = True
    End If
     If this is WM_QUERYENDSESSION, the closing event should be fired in the base WndProc
    MyBase.WndProc(m)
End Sub WndProc 
Private Sub Form1_Closing(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    If (systemShutdown) Then
     reset the variable since they may cancel the shutdown
        systemShutdown = False
        If (DialogResult.Yes = _
                MessageBox.Show("My application", "Would you care to save your work before logging off?", MessageBoxButtons.YesNo)) Then
                e.Cancel = True
        Else
                e.Cancel = False
        End If
    End If
End Sub
 
Madz!! Where did you find that!! Does it work for every operating system? (except win95 of course)
 
Last edited by a moderator:
Dear , this will not work with Windows 95,
with windows Me, 2000 or XP it will work, this uses WMI, i got it from Platform SDK
 
I dont know what WMI is.
You cannot even install the framework on 95. So I knew it would not work on that os. But what about 98?
 
I found in MSDN about WMI

Windows Management Instrumentation (WMI) is a scalable system management infrastructure that uses a single consistent, standards-based, extensible, object-oriented interface. WMI provides you with a standard way to interact with system management information and the underlying WMI APIs. WMI is used primarily by system management application developers and administrators to access and manipulate system management information.

WMI can be used to build tools that organize and manage system information so that administrators or system managers can monitor system activities more closely. For example, you can develop an application using WMI that pages an administrator when a Web server crashes.

and regarding to Windows 98 since long long time i havnt used it. and there is one thing .NET applications perform very slowly on Windows 98. it better to use Windows XP or 2000 for this
 
its a Predefiend Type with in WMI. i m not sure how to use it but i will check. you need to check WMI documentations for the complete list of its types . once this type has been inherited we can use it under C#.
 
Back
Top