C# 如何判断应用程序空闲
v鼠来宝v 2008-10-15 09:19:49 大家好,
我在程序中,使用了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);
}