Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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'

C#
  • Replies 53
  • Created
  • Last Reply

Top Posters In This Topic

  • *Experts*
Posted

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

"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
Posted

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.

C#
Posted
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.
C#
Posted

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?

C#
Posted (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 by aewarnick
C#
  • 2 weeks later...
Posted

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

Posted

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.
Posted

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.

Posted
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.
C#
Posted
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.
Posted
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.
C#
Posted
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.

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...