大家好,
我在程序中,使用了application.idle消息,在消息里面我有一个循环,来检测还有没有消息,可是在我应用的第3方控件操作的时候,判断不出来,造成程序处理慢。        void Application_Idle(object sender, EventArgs e)
        {
            while (AppStillIdle())
            {
                if (OnDocumentUpdates != null)
                    OnDocumentUpdates(this,EventArgs.Empty);
            }        }        private bool AppStillIdle()
        {
            NativeMethods.Message msg;
            return !NativeMethods.PeekMessage(out msg, Handle, 0, 0, 0);
        }======================================================    public static class NativeMethods
    {
        [StructLayout(LayoutKind.Sequential)]
        public struct Message
        {
            public IntPtr hWnd;
            public UInt32 msg;
            public IntPtr wParam;
            public IntPtr lParam;
            public UInt32 time;
            public Point p;
        }        [SuppressUnmanagedCodeSecurity] // We won't use this maliciously
        [DllImport("User32.dll", CharSet = CharSet.Auto)]
        public static extern bool PeekMessage(out Message msg, IntPtr hWnd, uint messageFilterMin, uint messageFilterMax, uint flags);
    }

解决方案 »

  1.   

    如何判断应用程序为空闲呢class MainForm中的函数
    {
            void Application_Idle(object sender, EventArgs e) 
            { 
                while (AppStillIdle()) 
                { 
                    if (OnDocumentUpdates != null) 
                        OnDocumentUpdates(this,EventArgs.Empty); 
                }         }         private bool AppStillIdle() 
            { 
                NativeMethods.Message msg; 
                return !NativeMethods.PeekMessage(out msg, Handle, 0, 0, 0); 
            } 
    }
    应用程序启动
    {
                  Application.EnableVisualStyles(); // must be done before creating any forms
                Application.SetCompatibleTextRenderingDefault(false); // must be done before creating any forms            mainForm = new MainForm();
                Application.Run(mainForm);
    }
      

  2.   

    Application_Idle,是当应用程序没有接受到消息时(一定时间).才会引发的.
    你在里面while处理的话,哪会接到消息呢.
      

  3.   

    貌似应用程序会一直收到WM_Time消息的吧 这样可以判断应用程序空闲?
      

  4.   

    靠自己解决,不能给自己分,郁闷原来的内容:
    PeekMessage(out msg, Handle, 0, 0, 0); 
                         ******
    改之后:
    PeekMessage(out msg, IntPtr.Zero, 0, 0, 0);
                            ^^^^^^^^^^^^