c - SetWindowPos/ShowWindow with a timeout -


i'm using setwindowpos function automation task show window. know there 2 ways windows provides this:

  • synchronously: setwindowpos or showwindow.
  • asynchronously: setwindowpos swp_asyncwindowpos or showwindowasync.

now, i'd best of both worlds: want able show window synchronously, because i'd done when function returns. but don't want call hang process - if takes long, want able abort call.

now, while looking answer, thing come using separate thread , using sendmessagetimeout, then, if thread hangs, there's not can end except of terminateprocess, not clean solution.

i have seen this answer, far understand, has no alternative native winapi.

the answer in question linked loops until either desired condition occurs or timeout expires. uses sleep() every iteration avoid hogging processor. version winapi can written quite simply, follows:

bool showwindowandwait(hwnd hwnd, dword dwtimeout) {     if (iswindowvisible(hwnd)) return true;     if (!showwindowasync(hwnd, sw_show)) return false;     dword dwtick = gettickcount();     {         if (iswindowvisible(hwnd)) return true;         sleep(15);     } while (dwtimeout != 0 && gettickcount() - dwtick < dwtimeout);     return false; } 

unfortunately think best you're going get. sendmessagetimeout can't used purpose because (as far know anyway) there's no actual message send cause target window shown. showwindowasync , swp_asyncwindowpos both work scheduling internal window events, , api isn't publicly exposed.


Comments