Please, Help!

Hi, Coders

Please, help me with my system shutdown program!

I think I am doing everything just fine, but...

Here is my code:

public event SessionEndingEventHandler ShutDown;
...................................

public Form1()
{
this.ShutDown += new SessionEndingEventHandler(ShuttingDown);
}

void ShuttingDown(object sender, SessionEndingEventArgs e)
{
ShutDown(this,e);
}

void ButtonClick(object sender, System.EventArgs e)
{
ShuttingDown(this,new SessionEndingEventArgs(SessionEndReasons.SystemShutdown));
}

It compiles without errors but on clicking the button I get StackOverflowException:(
What is wrong with my code and how can I achieve shutting down the system in pure C#, without the win32 API ExitWindowsEx 

Thanks in advance!
Gogou


Answer this question

Please, Help!

  • RonMoreno

    i want  shutdown code with c#
  • Casey Quayle

    ok.  by clicking on buttonClick, it will activate ShuttingDown(....)

    Then, you cal the method ShuttingDown, that in turn passes ShutDown with the exact same parameters..

    What does ShutDown contain

    rkimble is right on his suggestion.  I just get really annoyed to see methods with one line.. that calls another method with the same parameters.  It really tends to annoy me heavily.

  • sailendra

    Here is a thread with two examples:

    http://www.windowsforms.com/Forums/ShowPost.aspx tabIndex=1&tabId=41&PostID=21756


    If you don't want to mess with the windows API stuff, then just call the program "shutdown.exe":

    System.Diagnostics.Process.Start("Shutdown.exe", "-s")

    Other options are available and listed on the thread linked above (you can also type "shutdown  " at a command prompt to display the options).

  • maddog0

    dam.. rkimble beat me to it :)


    you can also use  "-f" that will make the system go down fast..



  • Sebastian Salgado

    You're firing the ShutDown event from within the ShutDown event handler; that is recursive, and is why you get the stack overflow.

    I don't know of any non-Win32 way of shutting down the system, and would be rather suprised if there was; shutting down the system would be a rather large security issue even for a trusted application, and few apps need to do it anyway.

  • Bob Rivers

    run an external process

    System.Diagnostics.Process.Start("Shutdown.exe", "-s") 

    this will only work on xp and 2003

    you can copy the shutdown.exe file from xp and move it to windows 2000 if needed..

  • Please, Help!