C# System.Windows.Forms.Application.DoEvents()

cs开发中,有时候界面用线程还是会卡,因为要invoke实时更新窗口,从db读取数据。
用backgroundworker效果不理想,用线程也不理想。
加上这个效果好好多
System.Windows.Forsm.Application.DoEvents();

msdn:
当运行窗体时,会创建新窗体,然后窗体等待处理各种事件,而窗体每次处理事件的时候,都会处理完与
该事件关联的所有代码,才会去处理其它事件。这时其它事件将会在队列中等待。所以在处理事件的时候,程序并不进行响应,当然也不会进行窗口重绘。
但是使用System.Windows.Froms.Application.DoEvents()会使窗口处理其他事件,使窗体能够重绘。

瞟了下源码
Application.cs 源码地址 .net referencesource 1022行
//DotNet48ZDP2/Source/ndp/fx/src/winforms/Managed/System/Windows/Applications.cs

//就是处理消息队列中所有的消息
//Process all windows messages currently in the message queue 
public static void DoEvents()
{
    ThreadContext.FromCurrent().RunMessagelook(NativeMethods.MSOCM.msoloopDoEvents,null);
}

//按给出的理由来开始消息循环处理
//starts a message loop for the given readson 
interval void RunMessageLoop(int reason,ApplicationContext context)
{
	//ensure the we attempt to apply theming before doing anything
	//that might create a window 
	IntPtr userCookie = IntPtr.Zero;
	//看是否要处理主题风格
	if(userVisualStyles)
	{
		userCookie = UnsafeNativeMethods.ThemingScope.Activate();
	}
	try
	{
		//会循环处理消息 直到停止 while(continueLoop){ ... }
		//DispatchMessageA 或DispatchMessageW 调用win32api
		RunMessageLoopInner(reason,context);
	}
	finally
	{
		UnsafeNativeMethods.ThemingScopr.Deactivate(userCookie);
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值