systemevents.sessionended is not being caught or fired
<p>Please in my code i try to catch win32.systemevents.sessionended event to procede with saving of my app data by the end of session in case the app is not closed manually .. some time ago this has been workin and now that i had my project grown a lil it is not any more..? i have tried to find something meaningfull for few days but found nothing really.. when i try to catch another systemevent like MonitorResolutionChanged it works well but this one not. I have also tried to register within the mainWindow (app form ..), nothing :-(    Please any idea?
I think all the relevant information should be in the beginning till void Main but i put it all in case you would need or want to see more .. Thanx a lot  Tomas 
My code:</p>

<pre><code>using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Threading;
using MovablePython; // my own class within this project
using Avn; //my own referenced assembly 

namespace DirDist
{
     class Program
    {
        private static string appGuid = "Cddbserviceman";
        private static System.Windows.Forms.ContextMenu nIMenu;
        internal static System.Windows.Forms.NotifyIcon notifyIcon1;
        private static MenuItem showItem;
        public static MenuItem justCDsItem;
        private static MenuItem searchItem;
        private static MenuItem settingsItem;
        private static MenuItem quitItem;

        internal static Form1 mainWindow;
        private static Hotkey hk;
        internal static Registration.LicenceState mode; // app mode - registered/trial/blocked/demaged ..
        /// &lt;summary&gt;
        /// The main entry point for the application.
        /// &lt;/summary&gt;
        [STAThread]
        static void Main()
        { 
            using (Mutex mutex = new Mutex(false, appGuid))
            {
                if (!mutex.WaitOne(0, false))
                {
                    MessageBox.Show("CDDB is already running on your machine \n  (Check status bar for access ..)");
                    return;
                }
                GC.Collect();

                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);

                mode = Registration.Startup();

                Program.mainWindow = new Form1();
                mainWindow.Activate();
                //mainWindow.Validate();
                //mainWindow.Update();
                mainWindow.Visible = false;
                PutIcon();

                //Microsoft.Win32.SystemEvents.SessionEnded += SystemEvents_SessionEnded;
                Microsoft.Win32.SystemEvents.SessionEnded += new Microsoft.Win32.SessionEndedEventHandler(SystemEvents_SessionEnded);//**zkousime zda funguje pro hibernaci ..
                RegisterHotKey(true);   
                Application.Run();
            }   
        }

        static void SystemEvents_SessionEnded(object sender, Microsoft.Win32.SessionEndedEventArgs e)
        {
            //MessageBox.Show("SessionEnded fired");

            RegisterHotKey(false);

            notifyIcon1.Visible = false;
            notifyIcon1.Dispose();
            notifyIcon1 = null;


            if (!mainWindow.dBSaved) mainWindow.SaveDb(Form1.settings.dBPath);
            if (mainWindow.index != null) mainWindow.SaveIndex(Form1.settings.indexPath);
            Microsoft.Win32.SystemEvents.SessionEnded -= new Microsoft.Win32.SessionEndedEventHandler(SystemEvents_SessionEnded);
            mainWindow.Close();
        }

        // zaregistruje globalni hotkey ctrl+shift+F Pro hledani
        private static void RegisterHotKey(bool active)
        {
            if (!active)
            {
                if (hk != null) hk.Unregister();
            }
            else
            {
                if(hk ==null) hk = new Hotkey();

                hk.KeyCode = Keys.F;
                //hk.Windows = true;
                hk.Shift = true;
                hk.Control = true;
                //hk.Pressed += delegate { Console.WriteLine("Windows+1 pressed!"); };
                hk.Pressed += delegate { searchItemClick(new object(), new EventArgs()); };

                if (hk.GetCanRegister(mainWindow)) hk.Register(mainWindow);
                else ; // just do nothing
            }
        }

        private static void PutIcon()
        {
            if (notifyIcon1 == null)
            {   
                showItem = new MenuItem ("&amp;Show interface", new System.EventHandler (showInfaceClick));
                justCDsItem = new MenuItem ("&amp;Jus'CDs",new System.EventHandler ( justCDsClick));
                justCDsItem.Checked = Form1.settings.justCDs;
                searchItem = new MenuItem("Search CDDB",new System.EventHandler (searchItemClick));
                searchItem.Shortcut = Shortcut.CtrlShiftF;
                searchItem.ShowShortcut = true;
                settingsItem = new MenuItem("Settings", new System.EventHandler(settingsItemClick));
                quitItem = new MenuItem("&amp;Quit", new System.EventHandler(quitItemClick));

                nIMenu = new System.Windows.Forms.ContextMenu(new MenuItem[5] { showItem, justCDsItem, searchItem,settingsItem, quitItem });


                notifyIcon1 = new System.Windows.Forms.NotifyIcon();
                notifyIcon1.ContextMenu = nIMenu;
                notifyIcon1.Icon = new System.Drawing.Icon(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\Icon1.ico");
                //notifyIcon1.Icon = new System.Drawing.Icon(System.IO.Path.GetDirectoryName( 
                //System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase ) + "Icon1.ico");
                //notifyIcon1.Icon = new System.Drawing.Icon("Icon1.ico");
                notifyIcon1.DoubleClick += new EventHandler(notifyIcon1_DoubleClick);
                notifyIcon1.Visible = true;
            }
        }

        /* private static void notifyIcon1_MouseMove(object sender, MouseEventArgs mea)
         * aby to fungovalo je treba upravit contextmenu na contextmenustrip a taky ty items .. az nakonec
         * je tu kolem uz rozdelana priprava ..
        {
            notifyIcon1.ShowBalloonTip(2000,AppName,"Active",ToolTipIcon.None);
        } */

        // clicks on NotificationIcon context menu ..
        private static void showInfaceClick(object sender, EventArgs e)
        {
            mainWindow.tabControl1.SelectedIndex = 0;
            mainWindow.Show();
        }

        private static void justCDsClick(object sender, EventArgs e)
        {
            Form1.settings.justCDs = mainWindow.checkBox1.Checked = justCDsItem.Checked = !Form1.settings.justCDs;
            if (mainWindow.Visible) mainWindow.Update();
        }

        private static void searchItemClick(object sender, EventArgs e)
        {
            mainWindow.tabControl1.SelectedIndex = 1 ;
            //this.Size = new Size(this.Width, SystemInformation.PrimaryMonitorSize.Height);
            mainWindow.Location = new System.Drawing.Point(SystemInformation.PrimaryMonitorSize.Width - mainWindow.Width, SystemInformation.PrimaryMonitorSize.Height - mainWindow.Height);
            //mainWindow.Location = new System.Drawing.Point(880, 500);
            mainWindow.Show();
        }

        private static void settingsItemClick(object sender, EventArgs e)
        {
            mainWindow.tabPage3_GotFocus(new Object(), new EventArgs());
            mainWindow.tabControl1.SelectedIndex = 2;
            mainWindow.Show();
        }

        public static void quitItemClick(object sender, EventArgs e)
        {
            if (DialogResult.Cancel == MessageBox.Show("Really exit application and stop scanning?",Form1.AppName,MessageBoxButtons.OKCancel,MessageBoxIcon.Question)) return;
            if (!mainWindow.dBSaved) mainWindow.SaveDb(Form1.settings.dBPath);
            //if (mainWindow.index != null) mainWindow.SaveIndex(Form1.settings.indexPath);
            if (Form1.settings.fileIndex) mainWindow.SaveIndex(Form1.settings.indexPath);
            mainWindow.Close();
            mainWindow = null;
            notifyIcon1.Visible = false;
            Application.Exit();
        }

        static void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
            //if (!mainWindow.Visible) mainWindow.WindowState = FormWindowState.Normal; else mainWindow.WindowState = FormWindowState.Minimized;
            //if (!mainWindow.Visible) mainWindow.Show(); else mainWindow.Hide();
            if (!mainWindow.Visible) mainWindow.Visible = true; else mainWindow.Visible = false;      
        }
    }
}
</code></pre>
