简体   繁体   中英

C++ Winapi while-loop workaround?

So, I have this ordinary while loop (as shown) and while my program is running inside the while loop for an extended period of time, the window becomes unresponsive, saying "blah blah blah isn't responding." Well... I know in WinAPI I should use the SetTimer() function and WM_TIMER, but guess what! I'm programming for fun this time, and I would like to know if there is a workaround... such as putting something inside my while loop that will keep it "responsive". I am not trying to be destructive.

Here is the Code:

while (battleupdate == 1)
{
    RECT wrect;
    wrect.left   = 0;
    wrect.right  = 570;
    wrect.top    = 0;
    wrect.bottom = 432;

    if (FILLRECT == 0){
    FillRect(hdc, &wrect, (HBRUSH) GetStockObject (WHITE_BRUSH));
    cout << "battleupdated" << endl; FILLRECT = 1; }


    /*OPPONENT STATS*/        
    if (1 == 1) {   stringstream stream; stream << opname << "            ";        
        TextOut (hdc, 20, 50, stream.str().c_str(), 12); }
    //if (1 == 1) {   stringstream stream; stream << itemslot4name << "        ";        
        //TextOut (hdc, 465, 188, stream.str().c_str(), 12); }
    //if (1 == 1) {   stringstream stream; stream << itemslot4effect << "   ";        
        //TextOut (hdc, 465, 204, stream.str().c_str(), 4); }

        battleupdate = 1; 

        //I DON'T CARE IF MY DELAY CODE IS LAME YES I KNOW ABOUT SLEEP().
        while (delay > 0)
        {
            delay -= 1;
        }
        if (delay == 0)
        {
            delay = resetdelay;
        }
    }

Is what I'm asking possible?

I appreciate any answers, even "Nope can't do that, You better use WM_TIMER!" so thanks in advance for answers!

  • Operating system: windows 7 ultimate

  • Compiler: Dev-C++

  • Other: using winapi

As you already know, you should use timers to handle your logic...

But if you really do not want to do this, you can use "DoEvents" do keep the window "responsive"...

void DoEvents()
{
  MSG msg;
  BOOL result;

  while ( ::PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE ) )
  {
    result = ::GetMessage(&msg, NULL, 0, 0);
    if (result == 0) // WM_QUIT
    {                
      ::PostQuitMessage(msg.wParam);
      break;
    }
    else if (result == -1)
    {
       // Handle errors/exit application, etc.
    }
    else 
    {
      ::TranslateMessage(&msg);
      ::DispatchMessage(&msg);
    }
  }
}

And in your while-loop you just need to call "DoEvents" periodically...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM