主线程要执行一个复杂的操作还要响应界面操作

来源:互联网 发布:淘宝开店商品选择 编辑:程序博客网 时间:2024/05/29 12:58

例如: 主线程中一个对话框的按钮事件如下:

 

for(int i=0; i<1000; i++)
{
  Sleep(10);

   if(i%100)
  {
   MSG msg;
   while(::PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
   {
     if(!theApp.PumpMessage()) 
     {
      ::PostQuitMessage(0);
      return;
     }
   }
  }
}

 

如果将

 

   if(i%100)
  {
   MSG msg;
   while(::PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
   {
     if(!theApp.PumpMessage()) 
     {
      ::PostQuitMessage(0);
      return;
     }
   }
  }

 

去掉后,只有执行完该函数后 主线程的其他消息才能响应 导致拖动界面操作无法执行