0

I am writing a program for a company , I wrote a program for suppliers . Unfortunately , One of IT Managers saw that , employees don't close this program before shut down their computers and he said to me your application must close itself before shuting down . I told him , this is not a problem and windows close this program but he says I don't want to see (Windows [windows waiting for background programs to close]) .

After this I find this code :

        static void SystemEvents_SessionEnding(object sender, SessionEndingEventArgs e)
    {
        switch (e.Reason)
        {
            case SessionEndReasons.Logoff:
                Application.Exit();
                break;

            case SessionEndReasons.SystemShutdown:
                Application.Exit();
                break;
        }
    }

and then add it to Form_Load :

            SystemEvents.SessionEnding += SystemEvents_SessionEnding;

If you write a messagebox before Application.Exit() , It shows it without problem but , It doesn't work and I can see (windows waiting for background programs to close) again .

Why ?! Is there any problem in my code ?! Is there another way ?

thanks

Sinaw
  • 61
  • 2
  • 9
  • 1
    Does you rapplication close quickly when you close it "normally" - or does it take a considerable time for closing (e.g. active background threads)? – Bernhard Hiller Jul 07 '14 at 08:04
  • @BernhardHiller , It contain background threads before shut down – Sinaw Jul 07 '14 at 10:18
  • 1
    How are the background threads "ended" - do just kill them or wait for them to exit? I guess the problem lies within their handling at shutdown; a simple kill might be appropriate. – Bernhard Hiller Jul 07 '14 at 10:30
  • 1
    @BernhardHiller Yeah , I just kill them . I used a kill Process code in SystemEvents_SessionEnding() method , It doesnt work , and I think before killing or closing my process ... Windows understands to a process in background is running and if I kill this process , Windows doesnt understand it closed . What do you think ?! Am I right ? – Sinaw Jul 07 '14 at 11:42

2 Answers2

1

As this post Is there a way in c# to detect a Windows shutdown/logoff and cancel that action (after asking the user) said, seems it cannot pause the time-out issue.

Maybe you can try the example in MSDN SessionEnding:

private static int WM_QUERYENDSESSION = 0x11;
private static bool systemShutdown = false;
protected override void WndProc(ref System.Windows.Forms.Message m)
{
    if (m.Msg==WM_QUERYENDSESSION)
    {
        MessageBox.Show("queryendsession: this is a logoff, shutdown, or reboot");
        systemShutdown = true;
    }

    // If this is WM_QUERYENDSESSION, the closing event should be
    // raised in the base WndProc.
    base.WndProc(ref m);

} //WndProc 

private void Form1_Closing(
    System.Object sender, 
    System.ComponentModel.CancelEventArgs e)
{
    if (systemShutdown)
        // Reset the variable because the user might cancel the 
        // shutdown.
    {
        systemShutdown = false;
        if (DialogResult.Yes==MessageBox.Show("My application", 
            "Do you want to save your work before logging off?", 
            MessageBoxButtons.YesNo))
        {
            e.Cancel = true;
        }
        else
        {
            e.Cancel = false;
        }
    }
}

Shutdown Changes for Windows Vista may provide more information for shutdown timeout

Community
  • 1
  • 1
Prisoner
  • 1,839
  • 2
  • 22
  • 38
0

Try Environment.Exit(0); instead of Application.Exit();