aewarnick Posted May 12, 2003 Author Posted May 12, 2003 private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e) { MessageBox.Show("closed "+System.Environment.HasShutdownStarted); e.Cancel=true; this.Visible=false; } D:\My Documents\C Sharp projects\WallPaperRotator\Form1.cs(873): An object reference is required for the nonstatic field, method, or property 'System.Environment.HasShutdownStarted' Quote C#
*Gurus* divil Posted May 12, 2003 *Gurus* Posted May 12, 2003 Weird, I guess they made it static in .NET 1.1. Since that class cannot be instantiated in .NET 1.0 I don't know what to suggest. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
*Experts* Nerseus Posted May 12, 2003 *Experts* Posted May 12, 2003 I've only read the last half of this thread, but I hope this helps :) The following link shows a way to see what's closing your form (code, the X, windows shutting down). It relies on what I think is a hack, but has been consistent on every machine I've tried. Just a suggestion if nothing else works: http://www.codeproject.com/useritems/FormClosing.asp -Nerseus Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
aewarnick Posted May 12, 2003 Author Posted May 12, 2003 I am downloading 1.1 but I will try both and post back. Quote C#
aewarnick Posted May 15, 2003 Author Posted May 15, 2003 Ok, here is the scoop: the stack trace method does not work every time on my computer. Sometimes when I test it, the same message is produced (userclose) I think it is. The same message for the user trying to close the for as the computer shutting down. I just installed 1.1 but not visual studio won't load. I uninsalled the old framework and that is probably why. Did I need to keep that? The Environment.HasShutdownStarted method would not compile with the old and new framework in place. Now I can't even test to see if it works now with just 1.1. Vs says it cannot load the compiler now. How can I fix this? It says to reinstall Vs but I think it will put the old framework on there. Quote C#
*Gurus* divil Posted May 16, 2003 *Gurus* Posted May 16, 2003 You should have both frameworks installed. Large amounts of Visual Studio 2002 are written with version 1.0 of the framework and it needs it installed. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
aewarnick Posted May 16, 2003 Author Posted May 16, 2003 How do I tell it to use the new framework so that Environment.HasShutdownStarted works. I tried downloading and installing SDK 1.1 but that did not seem to help. Quote C#
*Gurus* divil Posted May 16, 2003 *Gurus* Posted May 16, 2003 You can't. If you want a development environment to compile code for .NET 1.1 you need Visual Studio .NET 2003. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
aewarnick Posted May 16, 2003 Author Posted May 16, 2003 I can just get an upgrade for the standard version for 30 bucks right? Quote C#
aewarnick Posted May 16, 2003 Author Posted May 16, 2003 This is my last option (I guess) before I buy VS2003 for 30 (if it is 30) Can I make this work in some way? System.Environment E=new System.Environment(); if(E.HasShutdownStarted) I always get the error: D:\My Documents\C Sharp projects\WallPaperRotator\Form1.cs(1022): 'System.Environment.Environment()' is inaccessible due to its protection level Why would it be there if I can't use it? Quote C#
*Gurus* divil Posted May 16, 2003 *Gurus* Posted May 16, 2003 I honestly have no idea. I can only imagine it's a bug, that's why it's fixed in version 1.1. You might be able to instantiate the class under 1.0 via reflection. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
aewarnick Posted May 16, 2003 Author Posted May 16, 2003 (edited) I had to change the code some to get it to work correctly. Does this look right? private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e) { try{ StackTrace Trace = new StackTrace(7); StackFrame Frame = Trace.GetFrame(7); string s= Frame.GetMethod().Name; if(s=="WndProc") { Application.Exit(); } else { e.Cancel=true; this.Visible=false; } } catch{} } No need to respond if it looks ok. Edited May 16, 2003 by aewarnick Quote C#
eran Posted May 31, 2003 Posted May 31, 2003 Is it only for Windows Application or also for Console Application? Quote
aewarnick Posted May 31, 2003 Author Posted May 31, 2003 (edited) Probably. Try it out and see. Edited May 31, 2003 by aewarnick Quote C#
Winston Posted June 1, 2003 Posted June 1, 2003 hey i have that problem too!! i used divil's method for vb.net but it doesnt work this is how it goes for my on closing event Private Sub frmMain_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing If System.Environment.HasShutdownStarted = True Then Application.Exit() Else If Schedular.Settings.CloseButton = True Then 'Checks if the Close button setting is true 'If it is then e.Cancel = True 'Make close button to false, which is making it, it's default role If MessageBox.Show("Are you sure you want to exit Task Schedular? If Task Schedular is closed all tasks will be inactive", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) = DialogResult.Yes Then Schedular.Settings.StartedIntoTray = False Schedular.Main.XMLCollection_Save() Me.Dispose() 'Disposes the form End 'Ends the entire application Else End If Else 'If the close button setting is false then e.Cancel = True 'Make the close button to true, which is making it's role custom Select Case Schedular.Settings.ShowFrmMin 'Selects the case of the setting ShowFrmMin Case True 'If the setting is true then Me.Hide() 'Hide the main form Case False 'If the setting is false then Me.Hide() 'Hide the form Schedular.Forms.frmMain.ctxtTrayMenu.MenuItems.Item(0).Visible = False Schedular.Forms.frmMain.ctxtTrayMenu.MenuItems.Item(1).Visible = False Schedular.Forms.frmMain.ctxtTrayMenu.MenuItems.Item(2).Visible = False 'Disable the menu items Schedular.Forms.frmMain.TrayIcon.ShowBalloon(NotifyIcon2.NotifyIcon2.NotifyIcon2.EBalloonIcon.Info, "Task Schedular is still running!", "Palladium Info", 15000) 'Display the balloon tip Schedular.Forms.frmmin = New frmMin 'Instantiate the form Schedular.Forms.frmmin.TopMost = True 'Make the form always on top Schedular.Forms.frmmin.ShowDialog() 'Display the the form as modal form Schedular.Forms.frmmin = Nothing End Select End If End If End Sub does anyone know whats the problem? it seems fine Quote
Winston Posted June 1, 2003 Posted June 1, 2003 hey the hasshutdown started doesn't detect for windows shutdown it only accounts for the CLR i found a section where msdn talks about it it worked for me very well im sure u can translate it the code is in vb.net' here's the link http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfmicrosoftwin32systemeventsclasssessionendingtopic.asp Quote
eran Posted June 1, 2003 Posted June 1, 2003 Why did they publish in the MSDN the protected Methods I honestly have no idea. I can only imagine it's a bug, that's why it's fixed in version 1.1. You might be able to instantiate the class under 1.0 via reflection. Quote
*Gurus* divil Posted June 1, 2003 *Gurus* Posted June 1, 2003 The documentation in MSDN is automatically generated from the source code. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
guardian93 Posted June 2, 2003 Posted June 2, 2003 divil, how exactly do i "listen" for the SystemEvents.SessionEnding event ? this is not an form event. Quote
eran Posted June 2, 2003 Posted June 2, 2003 I think you can't. The same as you can't use MessageBox in console apps? I guess you can't use some classes in console while you can in windows app and vise versa... In any case, you will have to use threads, because the app terminates if not haven't some kind of event in the background. when I added the thread in my app and tried to log off, I got a message from windows that my program is not responding, even though I aborted the threads. In some reason, the SessionEnding event didn't trigger. BUG? don't know. Quote
aewarnick Posted June 2, 2003 Author Posted June 2, 2003 The SessionEnding event did not trigger becuase Closing executes first, terminating the app. You put SessionEnding in your code just like any other event handler. Quote C#
eran Posted June 2, 2003 Posted June 2, 2003 I think that there is a difference when you try the sessionending event when you are using the exe file or when you are testing it using the .NET compiler. Quote
aewarnick Posted June 2, 2003 Author Posted June 2, 2003 Maybe you have tested it more intensely than I did but I found that what I posted above was true. Because when I did not cancel the closing event SessionEnding was fired. Quote C#
eran Posted June 2, 2003 Posted June 2, 2003 Oh, I wasn't talking about the closing event. Actually I don't even use it. I just said that Ii so different behavior when i used the exe file or by running the app from the .NET workspace. Quote
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.