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);
}
...全文
472 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
禀赋苍天 2011-08-17
  • 打赏
  • 举报
回复
嘎嘎,这个能有用不
wangxiao2008 2008-10-16
  • 打赏
  • 举报
回复
帮你顶起来。顺便学习。
v鼠来宝v 2008-10-16
  • 打赏
  • 举报
回复
靠自己解决,不能给自己分,郁闷

原来的内容:
PeekMessage(out msg, Handle, 0, 0, 0);
******
改之后:
PeekMessage(out msg, IntPtr.Zero, 0, 0, 0);
^^^^^^^^^^^^
minioreo 2008-10-16
  • 打赏
  • 举报
回复
如果自己解决可以向版主申请分给自己的
greystar 2008-10-15
  • 打赏
  • 举报
回复
Application_Idle,是当应用程序没有接受到消息时(一定时间).才会引发的.
你在里面while处理的话,哪会接到消息呢.
jietuan 2008-10-15
  • 打赏
  • 举报
回复
没有使用过这种功能,帮你顶一下!
falx2004 2008-10-15
  • 打赏
  • 举报
回复
损招一个,检查使用内存多少~~~ - -!
minioreo 2008-10-15
  • 打赏
  • 举报
回复
mark
wanghaolincn 2008-10-15
  • 打赏
  • 举报
回复
学习。
v鼠来宝v 2008-10-15
  • 打赏
  • 举报
回复
如何判断应用程序为空闲呢

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);
}
minioreo 2008-10-15
  • 打赏
  • 举报
回复
貌似应用程序会一直收到WM_Time消息的吧 这样可以判断应用程序空闲?
jimmyzhang 2008-10-15
  • 打赏
  • 举报
回复
學習.
v鼠来宝v 2008-10-15
  • 打赏
  • 举报
回复
我需要在程序不空闲的时候不停的做一些东西
而程序非空闲的时候,不做

如何操作呢,有没有好的方法

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧