如题。
我做了个C#的托盘程序,当我将主窗体最小化到托盘后,
关闭操作系统,这时操作系统无法关闭,这是什么原因?有什么解决的方法吗?谢谢大家喽!

解决方案 »

  1.   

       private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                if (e.CloseReason == CloseReason.WindowsShutDown) Application.DoEvents();        }
    看看 
      

  2.   


    我的代码:        //最小化到托盘里了.
            private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
            {
                e.Cancel = true;
                this.WindowState = FormWindowState.Minimized;
                this.Hide();
                this.notifyIcon1.Visible = true;
            }        //为托盘图标添加了右键功能,点击 "退出" 事件
            private void menuItemExit_Click(object sender, EventArgs e)
            {
                this.notifyIcon1.Visible = false;
                this.Close();
                this.Dispose();
                Application.Exit();
            }
    我是在 notifyIcon 里添加了一个contextMenuStrip1,加了两个菜单,退出功能如上.
    出现问题也是关不了机,得先关了它,才能关机.没有直接写到from的Closing事件里.
    这种情况这怎样写,才能让它可以关机呢.
      

  3.   

    不管是Application.DoEvents(); 
    还是Application.Exit(); 程序始终都是运行着的????
      

  4.   

     private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
            {
    if (e.CloseReason == CloseReason.WindowsShutDown)
    {
     Application.Exit(); 
    }
    else
    {
                e.Cancel = true;
                this.WindowState = FormWindowState.Minimized;
                this.Hide();
                this.notifyIcon1.Visible = true;
    }
            }
    这样不行....??? 难道要Kill进程?
      

  5.   

    我给主界面中添加了一个方法
    private bool osShutDown=false;protected override void WndProc(ref Message m)
            {
                if (m.Msg == 0x0011)//WM_QUERYENDSESSION
                {
                    osShutDown = true;
                }
                base.WndProc(ref m);
            }
     private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
            {
                if (!osShutDown)
                {
                   this.notifyIcon1.Visible = false;
                }
            }
    这样就可以了
      

  6.   

    protected override void WndProc(ref Message m) 
            { 
                if (m.Msg == 0x0011)//WM_QUERYENDSESSION 
                { 
                    osShutDown = true; 
                } 
                base.WndProc(ref m); 
            } 这个方法不是很理解,有谁知道吗?感激了。
      

  7.   


    这办法治标不治本,我估计是lz哪里写过的线程没结束导致的,
    WM_QUERYENDSESSION是当系统关机时给所有窗体发的消息
    WndProc 是重载当前窗体的消息过程函数,用来拦截当前窗体收到的消息