Winapi c++: DialogBox hangs when breaking a loop -


i've created s simple w32 application cicle through 2 applications(hard coded window classes atm). when call start button (idc_start) works fine, when change focus hwnd application hangs cannot closed. need simple , clean method stop loop, start when calling idc_start. apreciated!

#include <stdio.h> #include <stdlib.h> #include <conio.h> #include <windows.h> #include <resource.h> #include <iostream>  using namespace std;   bool callback dlgproc(hwnd hwnd, uint message, wparam wparam, lparam lparam) {  hwnd hwnd1,hwnd2;  hwnd1 = findwindow("notepad++",0);  hwnd2 = findwindow("notepad",0);  bool bdone;   switch(message) {     case wm_initdialog:         // set dialog box, , initialise         default values         {         setdlgitemint(hwnd, idc_number, 5, false);          hicon hicon, hiconsm;         hicon = (hicon)loadimage(null, "e32.ico", image_icon, 32, 32, lr_loadfromfile);         if(hicon)         sendmessage(hwnd, wm_seticon, icon_big, (lparam)hicon);         else         messagebox(hwnd, "could not load large icon!", "error", mb_ok | mb_iconerror);         hiconsm = (hicon) loadimage(null, "e16.ico", image_icon, 16, 16, lr_loadfromfile);         if(hiconsm)         sendmessage(hwnd, wm_seticon, icon_small, (lparam)hiconsm);         else         messagebox(hwnd, "could not load small icon!", "error", mb_ok | mb_iconerror);         }     break;     case wm_command:         switch(loword(wparam))         {             case idc_start:             {                  bool bsuccess;                 int ndelay = getdlgitemint(hwnd, idc_number, &bsuccess, false);                 ndelay= ndelay*1000;                 int i=1;                 showwindow(hwnd,sw_minimize);                 if(bsuccess)                  {                   if (hwnd1 != 0&&hwnd2 != 0)                           {                            setthreadexecutionstate(es_continuous | es_display_required);                                 while(i)                                  {                                          if(bdone!=true)                                          {                                 setdlgitemint(hwnd, idc_showcount, ndelay/1000, false);                                 sleep(ndelay);                                 setforegroundwindow(hwnd1);                                 sleep(ndelay);                                       setforegroundwindow(hwnd2);                                 i++;                                                                }                                                              else                                           {                                                                   setthreadexecutionstate(es_continuous);                                 messagebox(hwnd, "stopped", "warning", mb_ok);                                 break;                                            }                                }                           }                 else                  {                      messagebox(hwnd,"cannot find suitable window","appdia",mb_ok);                  }                  }                 else                  {                     messagebox(hwnd, "number not identified", "warning", mb_ok);                 }              }             break;              case idc_stop:             bdone==true;             break;        }             break;     case wm_close:         enddialog(hwnd, 0);     break;     default:         return false; } return true; }   int winapi winmain(hinstance hinstance, hinstance hprevinstance, lpstr lpcmdline, int ncmdshow)  { return dialogbox(hinstance, makeintresource(idd_main), null, dlgproc);  } 

your logic loops sleeps, doesn't give dialog box way process it's messages, seems hang -- try using timer instead, like:

static bool bwnd1 = true; case wm_command:     switch(loword(wparam))     {   case idc_start:         {   int ndelay = getdlgitemint(hwnd, idc_number, &bsuccess, false);             ndelay= ndelay*1000;             showwindow(hwnd,sw_minimize);             settimer(hwnd, 1, ndelay, null);             break;         }          case idc_stop:             killtimer(hwnd, 1);             break;     }     break;  case wm_timer: {    hwnd hwnd = (bwnd1 ? findwindow("notepad++",0) : findwindow("notepad",0));      setforegroundwindow(hwnd);      bwnd1 = !bwnd1;      break; }  case wm_close:     killtimer(hwnd, 1);     enddialog(hwnd, 0);     break;  default:     return false; 

this sample code, you'll need add error checking it...


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -