- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
It took me a while to figure this out, but the "wait up to a full day" thing is just a smokescreen. Here is The Real WTF:
If avoidWindow equals IntPtr.Zero (as it is if you call the 2-arg version of GetWindow), then this check will always fail, and the it will stay in the while loop for the entire timeout period, even if it finds the window before then.
In other words, if you call the 2-arg version of GetWindow, and it doesn't find the window immediately (the statement before entering the while loop), it will wait the whole 24 hours.
Admin
window == IntPtr.Zero; //false, it equals the non-zero window value. window != IntPtr.Zero; //true. window == avoidWindow; //false, it equals the non-zero window value.
So that is false || (true && false) false || false false
so the while loop terminates.
Admin
The real wtf is assuming that a day has 86400 seconds. Due to daylight savings, they may have 82800 or 90000 seconds. Due to the occasional leap second, some days even have 86401 seconds.
Admin
Only on the day when the clock shifts back from DST to normal time... And when DST starts, the day has only 79200 seconds.
Since we're on the topic of daylight saving time:
I almost died laughing when I read this :)
Admin
Hear fjuckin' hear! <standing ovation> At last, someone's got it. There's no real WTF here, just a rather overly-long timeout that you would expect in practice will never come into play.
Jeebus, is our industry in a sorry state.
Admin
Admin
Admin
Admin
The only WTF I can see is that the timeout is enforced using a fixed number of 1/4 second thread sleeps (which ends up becoming more and more inaccurate) instead of just checking the current time vs. the time elapsed from the start of the call.
Other than that, it seems like a normal function with an overly-long default timeout.
Admin
There are a lot of WTFs in here:
Hardcoding a value (86400) all over the source code instead of using const int.
Using 250 ms Sleep() instead of say 3 or 5 seconds so you don't burn so much CPU cycles.
If the caller passes window name as a parameter to FindWindow(), if the window belongs to the same process and if it is unresponsive, the caller of FindWindow() will also become unresponsive.
If this function is by any chance called from an UI thread, Sleep() is a Bad Thing(TM).
This code searches only top-level windows
If the className and title are both NULL, all top-level windows will match and if avoidWindow is specified (not NULL) it will perform linear search through all the top-level windows every 250 ms until the right window is found
There are other, more efficient ways to communicate between processes and threads than by looking for window handle and using it to send messages.
Finally, the code comment in the OP is misleading since in 99% of the cases this code won't wait for 24 hours.
Admin
I think that would be more like 4 days, and possibly longer because of the 250 millisecond sleep that was added in there.
Admin