eran Posted May 30, 2003 Posted May 30, 2003 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? Quote
aewarnick Posted May 30, 2003 Posted May 30, 2003 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. Quote C#
eran Posted May 31, 2003 Author Posted May 31, 2003 It looks like microsoft could have add some more info on these events. I find that the .NET MSDN has a lack of information. Quote
aewarnick Posted May 31, 2003 Posted May 31, 2003 You're right about that. And their code on should be a different font and color coded like the program does. That's my opinion. Quote C#
eran Posted May 31, 2003 Author Posted May 31, 2003 (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 May 31, 2003 by eran Quote
aewarnick Posted June 1, 2003 Posted June 1, 2003 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) { } Quote C#
eran Posted June 1, 2003 Author Posted June 1, 2003 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. Quote
Winston Posted June 1, 2003 Posted June 1, 2003 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 Quote
eran Posted June 1, 2003 Author Posted June 1, 2003 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" Quote
Winston Posted June 1, 2003 Posted June 1, 2003 actually no he just continued his next question in that old thread Quote
eran Posted June 1, 2003 Author Posted June 1, 2003 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. Quote
aewarnick Posted June 1, 2003 Posted June 1, 2003 (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 June 1, 2003 by aewarnick Quote C#
Madz Posted June 1, 2003 Posted June 1, 2003 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 Quote The one and only Dr. Madz eee-m@il
aewarnick Posted June 1, 2003 Posted June 1, 2003 (edited) Madz!! Where did you find that!! Does it work for every operating system? (except win95 of course) Edited June 2, 2003 by aewarnick Quote C#
Madz Posted June 2, 2003 Posted June 2, 2003 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 Quote The one and only Dr. Madz eee-m@il
aewarnick Posted June 2, 2003 Posted June 2, 2003 I don't 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? Quote C#
Madz Posted June 2, 2003 Posted June 2, 2003 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 Quote The one and only Dr. Madz eee-m@il
Madz Posted June 2, 2003 Posted June 2, 2003 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#. Quote The one and only Dr. Madz eee-m@il
aewarnick Posted June 2, 2003 Posted June 2, 2003 I think H is hex because when I put in 17, the hex for 11 the statement was true. Quote C#
Madz Posted June 2, 2003 Posted June 2, 2003 Might be possible You are genisue dear. ;) Quote The one and only Dr. Madz eee-m@il
aewarnick Posted June 2, 2003 Posted June 2, 2003 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. Quote C#
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.