Comment On Patiently Waiting Forever

Today's snippet comes from an issue that Brian discovered in the production code of a large telecomunication company's call center software. It attempts to solve a fairly simple problem: get the handle to a specified window and, if it can't be found, try again for MAX_SECONDS. However, there was a bit of an issue with the default wait time... [expand full text]
« PrevPage 1 | Page 2Next »

Re: Patiently Waiting Forever

2007-08-20 09:34 • by Tim (unregistered)
They better have some damn good hold music if i'm waiting that long

Re: Patiently Waiting Forever

2007-08-20 09:38 • by Frank (unregistered)
150307 in reply to 150304
What's with the int i=1; It's never used!

Re: Patiently Waiting Forever

2007-08-20 09:39 • by nobody (unregistered)
150308 in reply to 150304
Tim:
They better have some damn good hold music if i'm waiting that long

I'm sure they have something like "The Best of Captain Beefheart" or maybe they went in a different direction with "Lawrence Welk's Greatest Hits"

Re: Patiently Waiting Forever

2007-08-20 09:40 • by Volmarias
150309 in reply to 150307
Frank:
What's with the int i=1; It's never used!


It's in the while loop.

Re: Patiently Waiting Forever

2007-08-20 09:40 • by keezer (unregistered)
Arrrgghhh. How about "Do you know the way to San Jose?" arranged for synth sax mixed with the 30 second chiming in of the company message, "Your call is important to us. Please hold and a representative will be with you shortly."

Re: Patiently Waiting Forever

2007-08-20 09:40 • by ucltech
Let's pass in a value of 86399 seconds and see what happens.

Re: Patiently Waiting Forever

2007-08-20 09:43 • by lightinthedark (unregistered)
From the looks of it, if you tried to tell it to wait more than a day it'd ignore you and just set max to 86400 * 4 again. How rude! :-)
And the i=0 initialises the counter that gets checked down near the bottom in the while loop ... i < max ...

Hello
:-Lightinthedark

Re: Patiently Waiting Forever

2007-08-20 09:44 • by XIU
150313 in reply to 150311
ucltech:
Let's pass in a value of 86399 seconds and see what happens.


It will wait 23 hours, 59 minutes and 59 seconds...

Re: Patiently Waiting Forever

2007-08-20 09:44 • by somebody (unregistered)
Erm, 6 hours.
And window != IntPtr.Zero seems abusive here.

Re: Patiently Waiting Forever

2007-08-20 09:46 • by mrsticks1982

int max = 86400 * 4;
...
&& (i++ < max))
...
Thread.Sleep(250);


they better have one fantastic sales pitch to keep someone on the phone for a week!

Oblig. Simpsons Quote

2007-08-20 09:53 • by jgayhart
You can't put me on hold, I'll put you on hold. (singing) I am a lineman for the county. (speaking) Your call is important to us. Please continue to hold. (singing) And I drive the main road. (speaking) There are…eight calls ahead of you. (singing) And the Wichita lineman is still on the li-li-li-li-li-li-li-li-li-li-li-li-line.

24 hours.... no more like 96 hours

2007-08-20 10:24 • by Jimmy (unregistered)
// 86400 is the number of seconds in a day
int max = 86400 * 4;

Since they increase the number of seconds to wait by multiplying it with 4 wouldn't that mean that we wait 4 days instead of just one lousy day. :)

I can image the discussion about the wait time:
* Hmm how long should we keep trying?
- Well maybe a few minutes are enough.
* What if the system is VERY busy?
- Ok, what about max wait time of one hour?
* Set it to a full day and we'll get back to this.
...
later
- What was it that architect told me? Lets be on the
safe side and put the wait time to four days.
...
even later
- What was it we were supposed to change???

Re: Patiently Waiting Forever

2007-08-20 10:27 • by N2 (unregistered)
150320 in reply to 150319
Jimmy:
// 86400 is the number of seconds in a day
int max = 86400 * 4;

Since they increase the number of seconds to wait by multiplying it with 4 wouldn't that mean that we wait 4 days instead of just one lousy day. :)

I can image the discussion about the wait time:
* Hmm how long should we keep trying?
- Well maybe a few minutes are enough.
* What if the system is VERY busy?
- Ok, what about max wait time of one hour?
* Set it to a full day and we'll get back to this.
...
later
- What was it that architect told me? Lets be on the
safe side and put the wait time to four days.
...
even later
- What was it we were supposed to change???


Its not 4 days because it tries to find the window 4 times a second, and increments i with each one. (250ms = 1/4th second)

Re: Patiently Waiting Forever

2007-08-20 10:28 • by bstorer
150321 in reply to 150319
Jimmy:
// 86400 is the number of seconds in a day
int max = 86400 * 4;

Since they increase the number of seconds to wait by multiplying it with 4 wouldn't that mean that we wait 4 days instead of just one lousy day. :)

No, because they sleep for 250 milliseconds, which is .25 seconds.

Re: Patiently Waiting Forever

2007-08-20 10:31 • by Andrew (unregistered)
Perhaps it was BT?

http://www.news.com.au/story/0,23599,22270173-401,00.html

Re: Patiently Waiting Forever

2007-08-20 10:33 • by Ben Hutchings (unregistered)
Not only does it use an inappropriate time limit; but it uses FindWindow and ignores a certain result (avoidWindow). What if there are two matching windows and FindWindow always returns avoidWindow? I think that may well be why it doesn't return quickly. This should be using EnumWindows or whatever wrapper .NET has for it. Also, messing with other process's windows is something of a WTF (window handles aren't ref-counted!) though sadly it seems to be necessary too often on Windows.

Re: Patiently Waiting Forever

2007-08-20 10:33 • by Daniel Pitts (unregistered)
The Real WTF is the analysis of this code...

It tries every 1/4 second to find the window, for a full day. The fact that it gives a full day time before it gives up is a bit of a "Hmm", but not a WTF.

Captcha: poindexter

Re: Patiently Waiting Forever

2007-08-20 10:34 • by Crabs (unregistered)
Seems like you pass a maximum amount of time you're willing to wait. The maximum time that can be is 1 day. Doesn't seem so ridiculous...Yeah, a day's a bit long, but the max has to be somewhere, and this guy is expecting a smaller value to be passed. At least he's doing some sort of input validation.

Re: Patiently Waiting Forever

2007-08-20 10:41 • by Anonymous (unregistered)
The fact that it gives a full day time before it gives up is a bit of a "Hmm", but not a WTF.


You always give a non-responsive application 25 hours to recover before you declare it dead? I bow in respect of your patience.

Using an infinite loop would have had exactly the same effect for the user. Waiting a full day is clearly a WTF.

Re: Patiently Waiting Forever

2007-08-20 10:43 • by Evo
And as usual; the real WTFs are the comments..

Re: Patiently Waiting Forever

2007-08-20 10:49 • by homer (unregistered)
150329 in reply to 150318
jgayhart:
You can't put me on hold, I'll put you on hold. (singing) I am a lineman for the county. (speaking) Your call is important to us. Please continue to hold. (singing) And I drive the main road. (speaking) There are…eight calls ahead of you. (singing) And the Wichita lineman is still on the li-li-li-li-li-li-li-li-li-li-li-li-line.


Best obscure simpsons reference ever!

Re: Patiently Waiting Forever

2007-08-20 11:02 • by wtf (unregistered)
150331 in reply to 150327
But a full day is only the default, and the max accepted interval. Callers can provide a shorter interval if they want.

What's wrong with this?

It seems the real WTF isn't in the code shown, but in the calling function, and that it relies on the default. Still not a big deal.

A bigger WTF in my book is the way he measures time - a certain number of sleep(250) can't be accurate: clock resolution rounding might make each sleep actually 233 or 266ms or some other arbitrary period; the surrounding code also takes time to execute; there may be other delays caused by other processes or I/O; also, perhaps (not sure on windows) sleep can be interrupted earlier by signals and such.

Re: Patiently Waiting Forever

2007-08-20 11:06 • by Andrew (unregistered)
150334 in reply to 150304
Tim:
They better have some damn good hold music if i'm waiting that long


I think "The Theme From Shaft" is almost long enough that it won't repeat.

Re: Patiently Waiting Forever

2007-08-20 11:09 • by rdrunner
The code is already optimized and noone sa<w it. It uses the upper bound of the if statement. This means it will NOT multiply the timer by 4. So effectively it will only wait 8 H with the call...

But if you pass in 1 second LESS, then you will increase the waittime by almost 300%...

Re: Patiently Waiting Forever

2007-08-20 11:09 • by Andrew (unregistered)
Would this happen to be for the cable company?

Re: Patiently Waiting Forever

2007-08-20 11:13 • by anon (unregistered)
It's behavior is "I will wait a day unless you give me a time less than a day.", which is fine (other than being a large maximum).
i.e. It is setting "max" to be the 4 * minimum of (maxWaitInSeconds and dayInSeconds)

Re: Patiently Waiting Forever

2007-08-20 11:13 • by Andrew (unregistered)
150340 in reply to 150304
Tim:
They better have some damn good hold music if i'm waiting that long


There is a Tom Petty song that comes to mind too.

Re: Patiently Waiting Forever

2007-08-20 11:17 • by jkupski (unregistered)
150342 in reply to 150337
Andrew:
Would this happen to be for the cable company?


No. If it was, the max wait time would be random, and there would be a 20% chance of dropping your call on every iteration of the loop.

And remember, folks: when the cable company says "We'll be there Friday, 9-1" they aren't telling you when they will arrive... they're quoting odds.

Re: Patiently Waiting Forever

2007-08-20 11:26 • by Andrew (unregistered)
150344 in reply to 150342
jkupski:
Andrew:
Would this happen to be for the cable company?


No. If it was, the max wait time would be random, and there would be a 20% chance of dropping your call on every iteration of the loop.

And remember, folks: when the cable company says "We'll be there Friday, 9-1" they aren't telling you when they will arrive... they're quoting odds.


ROLFLMAO, coffee through nose, pee in my pants, and puke!

Re: Patiently Waiting Forever

2007-08-20 11:29 • by woofles (unregistered)
150345 in reply to 150336
rdrunner:
The code is already optimized and noone sa<w it. It uses the upper bound of the if statement. This means it will NOT multiply the timer by 4. So effectively it will only wait 8 H with the call...

But if you pass in 1 second LESS, then you will increase the waittime by almost 300%...


Try again: 'max' is initialized to '86400 * 4' so it will wait a day, not 8 hours.

Re: Patiently Waiting Forever

2007-08-20 11:35 • by Cloak (unregistered)
150347 in reply to 150328
Evo:
And as usual; the real WTFs are the comments..


lol

Re: Patiently Waiting Forever

2007-08-20 11:35 • by Cloak (unregistered)
150348 in reply to 150328
Evo:
And as usual; the real WTFs are the comments..


lol

Re: Patiently Waiting Forever

2007-08-20 11:36 • by dkf (unregistered)
150349 in reply to 150337
Andrew:
Would this happen to be for the cable company?
Or AT&T? (Motto: We don't have to care, we're the phone company.)

Re: Patiently Waiting Forever

2007-08-20 11:49 • by nobody (unregistered)
150354 in reply to 150349
dkf:
Andrew:
Would this happen to be for the cable company?
Or AT&T? (Motto: We don't have to care, we're the phone company.)

For a while, ATT No-Broadband was my cable company.
A few people actually were able to get to customer "service", after waiting over 40 minutes. Just about everyone else gave up.

Re: Patiently Waiting Forever

2007-08-20 12:03 • by Mexi-Fry (unregistered)
public static IntPtr GetWindow(string className, string title,
int maxWaitInSeconds, IntPtr avoidWindow)
{
IntPtr window = IntPtr.Zero;
int max = 86400 * 4;
int i = 0;

// 86400 is the number of seconds in a day

if (maxWaitInSeconds < 86400)
{
max = maxWaitInSeconds * 4;
}

window = Win32API.FindWindow(className, title);

while ( ((window == IntPtr.Zero)
|| (window != IntPtr.Zero && window == avoidWindow))
&& (i++ < max))
{
Thread.Sleep(250);
window = Win32API.FindWindow(className, title);
}
return window;
}


Actually, this code seems to suggest that it will wait up to a day for a handle to a window. It apparently tries 4 times per second. A bit silly, but this seems like a small matter compared to enum x {yes=0,no=1}

Re: Patiently Waiting Forever

2007-08-20 12:07 • by Mexi-Fry (unregistered)
150358 in reply to 150357
Mexi-Fry:
public static IntPtr GetWindow(string className, string title,
int maxWaitInSeconds, IntPtr avoidWindow)
{
IntPtr window = IntPtr.Zero;
int max = 86400 * 4;
int i = 0;

// 86400 is the number of seconds in a day

if (maxWaitInSeconds < 86400)
{
max = maxWaitInSeconds * 4;
}

window = Win32API.FindWindow(className, title);

while ( ((window == IntPtr.Zero)
|| (window != IntPtr.Zero && window == avoidWindow))
&& (i++ < max))
{
Thread.Sleep(250);
window = Win32API.FindWindow(className, title);
}
return window;
}


Actually, this code seems to suggest that it will wait up to a day for a handle to a window. It apparently tries 4 times per second. A bit silly, but this seems like a small matter compared to enum x {yes=0,no=1}

Clarification: UP TO... to be sure that I said it right. If the window was closed and is no longer retrievable, then the application will never pass that point, but if it manages to get a reference in the first second, or ten minutes it will. Still seems silly to me... but a training issue could get around that bug.

Re: Patiently Waiting Forever

2007-08-20 12:24 • by Zygo (unregistered)
TRWTF is the math skills of the people commenting:

1. There are 86400 seconds in a week.
2. There are 31 hours, 59 minutes, 59.75 seconds in a day.
3. Code on Windows that calls Sleep(250) 86400 times will take only one day to execute.

Re: Patiently Waiting Forever

2007-08-20 12:24 • by Seb (unregistered)

http://www.shortnews.com/start.cfm?id=64390

Re: Patiently Waiting Forever

2007-08-20 12:28 • by Andrew (unregistered)
150365 in reply to 150349
dkf:
Andrew:
Would this happen to be for the cable company?
Or AT&T? (Motto: We don't have to care, we're the phone company.)


Well, there's a missing segment of code


while (customer.minutesLeft() > 0)
{
music.play();
announcer.annoy(); // Tells the customer how important they are
ringer.trickRing();
}

Re: Patiently Waiting Forever

2007-08-20 12:30 • by Andrew (unregistered)
150366 in reply to 150362
Zygo:
TRWTF is the math skills of the people commenting:

1. There are 86400 seconds in a week.
2. There are 31 hours, 59 minutes, 59.75 seconds in a day.
3. Code on Windows that calls Sleep(250) 86400 times will take only one day to execute.


60 * 60 * 24 = 86400

Re: Patiently Waiting Forever

2007-08-20 12:49 • by Someone You Know
150372 in reply to 150362
Zygo:
1. There are 86400 seconds in a week.


Unless a week on your planet is equal to one Earth day, you need to check your math.

Re: Patiently Waiting Forever

2007-08-20 12:51 • by gomer (unregistered)
150373 in reply to 150366
Andrew:
Zygo:
TRWTF is the math skills of the people commenting:

1. There are 86400 seconds in a week.
2. There are 31 hours, 59 minutes, 59.75 seconds in a day.
3. Code on Windows that calls Sleep(250) 86400 times will take only one day to execute.


60 * 60 * 24 = 86400


Exactly. One week. Sheesh.

Re: Patiently Waiting Forever

2007-08-20 13:02 • by bkendig
150378 in reply to 150372
Someone You Know:
Zygo:
1. There are 86400 seconds in a week.


Unless a week on your planet is equal to one Earth day, you need to check your math.


He was remarking on "the math skills of the people commenting". I think he himself is aware of the correct significance of the number 86400.

Re: Patiently Waiting Forever

2007-08-20 13:12 • by herman (unregistered)
150380 in reply to 150362
Zygo:
TRWTF is the math skills of the people commenting:

1. There are 86400 seconds in a week.
2. There are 31 hours, 59 minutes, 59.75 seconds in a day.
3. Code on Windows that calls Sleep(250) 86400 times will take only one day to execute.
I knew the rotationspeed of the Earth was retarding but that it was now 8 hours longer then when I was born in 1951 I did not know. Thanks for this insight

Re: Patiently Waiting Forever

2007-08-20 14:16 • by AdT (unregistered)
This should actually wait longer than a day (unless it finds the window) because of scheduling delays and the time it takes to execute the loop 86400 * 4 times. 86400 seconds that's just the raw sleep() time.

herman:
I knew the rotationspeed of the Earth was retarding but that it was now 8 hours longer then when I was born in 1951 I did not know. Thanks for this insight


The times they are a'changin'.

Re: Patiently Waiting Forever

2007-08-20 14:18 • by jread
Yeah, count me in as another person who didn't know there were more than 24-hours in a day....

Re: Patiently Waiting Forever

2007-08-20 14:38 • by ThePants999
Does nobody understand Zygo's comment?

Re: Patiently Waiting Forever

2007-08-20 14:47 • by arty
150404 in reply to 150396
If we had 32 days, and the days were composed of 32 hours with 64 minutes, each containing 64 seconds, each containing 1024 milliseconds, then each month would start at 0x...00000000 and end at 0x...ffffffff. It'd be cool and thrilling and generally nifty, and programmers could tell the time.

My name is arty, and I approved this message.

Re: Patiently Waiting Forever

2007-08-20 15:22 • by Harrow (unregistered)
Assuming the following specification, the code is correct:

If you want to get the handle of a specified window by title, but not if it is a certain window handle you already have, and you only want to wait a specified number of seconds, then call

GetWindow( <classname>, <title>, <seconds>, <butNotThisOne> )

Note: If you pass a time period that is more than 24 hours, it will be truncated to 24 hours.

Your application never passes a time anywhere near that long. If it does, there's a coding error, and the very long default insures that you will notice the problem while testing (especially as this a telco, and "testing" is performed by the customers during the first few days after deployment).

But if you want to get the handle of a specified window by title, and will accept any window handle, and you are willing to wait "forever", then call

GetWindow( <classname>, <title> )

Note: it won't wait forever, but will wait 24 hours.

Since you only use this call one time, during application startup, there should be no problem, If the application fails to start, you have a whole day to hunt down the frozen process and find out what went wrong.

-Harrow.

Re: Patiently Waiting Forever

2007-08-20 16:23 • by Mogri (unregistered)
150423 in reply to 150373
Andrew:
Zygo:
TRWTF is the math skills of the people commenting:

1. There are 86400 seconds in a week.
2. There are 31 hours, 59 minutes, 59.75 seconds in a day.
3. Code on Windows that calls Sleep(250) 86400 times will take only one day to execute.


60 * 60 * 24 = 86400


It's odd that everyone gets hung up on point 1 and misses point 2. I'm not sure what Zygo was getting at, but he wasn't being serious. None of the three points above is correct.
« PrevPage 1 | Page 2Next »

Add Comment