Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi,

 

I looked in all forums. MSDN doesn't 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 didn't.

 

Any one knows how to impliment this event?

Posted

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.xtremedotnettalk.com/t71780-1.html

 

I had the same problem.

C#
Posted
It looks like microsoft could have add some more info on these events. I find that the .NET MSDN has a lack of information.
Posted (edited)

This is my code. In some re3ason, the file is not been created. also messagebox can't added to the "CloseEvent" function.

I also tried to use the "SessionEnding" event and the same results. I'll be thankfull if you help me.

 

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();
   }					
}

Edited by eran
Posted

First of all, I don't 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)

{

 

}

C#
Posted

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

e.cancel = true;

and in the end of the code I added

e.cancel = false;

I hope it didn't make the difference.

Posted

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

Posted

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"

Posted
Weird, It seems that the SessionEnding event when implemented in Console application doesn't behave as it should be. I get a message from my PC that the program is not responding.
Posted (edited)

You don't need e.Cancel=fasle at the end of your code.

This is what you are looking for:

 

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. Don't use SessionEnding.

Edited by aewarnick
C#
Posted

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]

 

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

The one and only

Dr. Madz

eee-m@il

Posted

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

The one and only

Dr. Madz

eee-m@il

Posted

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

The one and only

Dr. Madz

eee-m@il

Posted
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 it's types . once this type has been inherited we can use it under C#.

The one and only

Dr. Madz

eee-m@il

Posted

Actually I had no idea what hex numbers were until I looked on the net and used my windows calculator to find out what 11 was in hex. I put the int 17 in and it worked.

Not genious, just experimental.

C#

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...