DAY 004: _WINDOWHASFOCUS
#3
(11-09-2022, 04:42 PM)bplus Wrote: Might this one encounter a racing problem?

Sometimes I do a compile and Run and new .exe Window has focus for moment and then blinks back to IDE. Seems rare and random.

Maybe right at start up.  QB64 is rather odd in how it starts.  If you notice, we first pop up a console window for just a few moments -- at that point, we have a window handle which is a console handle.  Then we close that initial console and we still have the old handle, with no screen whatsoever.  Then we swap to the screen the user specifies at start up (usually with a SCREEN _NEWIMAGE() statement), and it takes just a moment for windows to register and return this new window handle to us...

So *anything* to do with window handles can get themselves lost in just about any of those moments at start up.  

Do we have a window handle?  Sure do!  We have one from the moment the first console window is created.

Is it a *VALID* handle?  Maybe.  Are we still at the stage where that console window is up and viable?  Has it closed already?  If it's closed, and we haven't gotten back the new handle from the OS for the new window yet, then we've got a dead handle now that basically goes nowhere.  Trying to get _WINDOWHASFOCUS on that dead handle is either going to return a zero -- "Nope, can't have focus on a closed window!" -- or else it's going to give some false result.  "Sure, this console is still in focus!  We haven't registered it closing yet!"

What I've found to be good advice:  When starting your program for the first time, place a small _DELAY once in your program before the *FIRST* command which may rely upon any form of a window handle.  Give the OS time to make that console, close it, and then create and register the window we're actually going to be working with.  You don't need a long delay -- usually _DELAY 0.2 is more than enough for the programs which I've written in the past.
Reply


Messages In This Thread
DAY 004: _WINDOWHASFOCUS - by SMcNeill - 11-09-2022, 01:26 PM
RE: DAY 004: _WINDOWHASFOCUS - by bplus - 11-09-2022, 04:42 PM
RE: DAY 004: _WINDOWHASFOCUS - by SMcNeill - 11-09-2022, 06:25 PM
RE: DAY 004: _WINDOWHASFOCUS - by mnrvovrfc - 11-09-2022, 06:28 PM
RE: DAY 004: _WINDOWHASFOCUS - by Pete - 11-10-2022, 12:00 AM
RE: DAY 004: _WINDOWHASFOCUS - by SMcNeill - 11-10-2022, 12:35 AM
RE: DAY 004: _WINDOWHASFOCUS - by Pete - 11-10-2022, 01:20 AM
RE: DAY 004: _WINDOWHASFOCUS - by SMcNeill - 11-10-2022, 02:17 AM
RE: DAY 004: _WINDOWHASFOCUS - by CharlieJV - 11-10-2022, 12:53 AM
RE: DAY 004: _WINDOWHASFOCUS - by Pete - 11-10-2022, 02:24 AM



Users browsing this thread: 2 Guest(s)