• Pawel (unregistered)

    If the sysadmin knows perl, he will know the sleep operation.

  • Sid2K7 (unregistered)

    Nice code. Next time, tell him to configure the "IdleProcess" to run for that amount of time ;)

    On another note: What happened to TheDailyWTF? Is this the same site? The logo seems somewhat familiar, but the layout and such are different. Granted, I haven't been here for a while, but replacing something as "The Daily WTF" (which has a very clear meaning) with the phrase "Worse Than Failure" (which probably is not used in IT as much as "WTF") is a step in the wrong direction.

    Then, again, the stories seem to be just as hilarious, so ill be back after my usual interval (1-2 months).

    Captcha: bathe - I'd better ....

  • Tom (unregistered)

    I think the wtf here would have to be converting server scripts to .NET.

    Honestly, if perl is the best tool for the job, why re-write the scripts?

  • Jimmy (unregistered)
    "the guy who coded this is technically not a developer"

    If that's the case, then I will submit my grandma's VB.NET WTF's, as she's also technically not a developer.

  • Dr. Advil (unregistered)

    Calling Thread.sleep() would allow other threads to run and may subject your current thread to undesired behavior via a scheduler that you have no control over.

  • Dr. Advil (unregistered) in reply to Dr. Advil

    but why did he have to use a double? if you want fractions of seconds just go with the standard milliseconds approach.

  • Will Perdikakis (unregistered)

    The code is actually pretty resourceful not a WTF, really.

    He knew what he wanted to do and rather than using the build-in thread function, he used a spin-lock. Lots more wasted CPU cycles, but virtually the same end results.

    How many times have you implemented something that already existed in a standard function library only to find out later?

  • nobody (unregistered)

    Last time I looked, Perl was available for Windows, with a free version, and if you wanted to spend the money, a full Visual Studio plug-in language.

    I haven't tried to use it in .NET, but I'd guess that it can be made to interoperate.

    And this function would be bad even in perl. "How come our server is so slow?"

  • Dave (unregistered)

    Windows Script Host (WSH) or the new PowerShell would be the best way to handle something like this. In WSH ou can get to nearly all system functionality via WMI or FileSystemObject.

  • vt_mruhlin (unregistered)

    Not knowing .NET that well, things like this confuse me: Convert.ToDateTime(DateTime.Now.TimeOfDay.ToString());

    It seems intuitive that DateTime.Now.TimeOfDay might already be a DateTime object, so converting it to a String, then back to a DateTime..... ?

  • Will (unregistered) in reply to vt_mruhlin
    vt_mruhlin:
    Not knowing .NET that well, things like this confuse me: Convert.ToDateTime(DateTime.Now.TimeOfDay.ToString());

    It seems intuitive that DateTime.Now.TimeOfDay might already be a DateTime object, so converting it to a String, then back to a DateTime..... ?

    Exactly, it's a waste of time...which is the point. It keeps the thread busy until the specified time has elapsed.

  • (cs) in reply to Will Perdikakis
    Will Perdikakis:
    The code is actually pretty resourceful not a WTF, really.

    He knew what he wanted to do and rather than using the build-in thread function, he used a spin-lock. Lots more wasted CPU cycles, but virtually the same end results.

    How many times have you implemented something that already existed in a standard function library only to find out later?

    I remember doing that in VB6 until I learned about the Sleep API.

    I did it in a server app that waited for files to process and the network admin asked me why my app was always taking 100% of the CPU. oops

  • George (unregistered) in reply to Will
    Will:
    vt_mruhlin:
    Not knowing .NET that well, things like this confuse me: Convert.ToDateTime(DateTime.Now.TimeOfDay.ToString());

    It seems intuitive that DateTime.Now.TimeOfDay might already be a DateTime object, so converting it to a String, then back to a DateTime..... ?

    Exactly, it's a waste of time...which is the point. It keeps the thread busy until the specified time has elapsed.

    The converts make the code, um, inelegant. But other than that, I've been seeing sleep loops since:

    10 FOR I = 1 TO 1000 20 NEXT I

    At least this will sleep for the same amount of time regardless of hardware platform.

  • (cs)

    I don't think this is fair to this poor guy. This sysadmin had been asked to leave his comfort zone and learn a new language (a difficult language at first) and put it in production. To expect results other than messed up code is foolish. The management or development team should have worked with him to find out what it was he was trying to do and help him. At the least they should have reviewed his code and gave him some pointers.

  • RON (unregistered)

    What I think is wrong:

    1. using a double for passing a time value to a function. Come on people, .NET has a TimeSpan class specifically for this reason. Using a TimeSpan saves clients of the code time since they no longer have to look up the function def to figure out if the time is in seconds/milliseconds/etc.

    2. Converting to/from strings when you already have DateTime's? WTF?

    3. Using a 100% CPU utilization loop to "pause" the system. Hooray. (idiot)

  • RON (unregistered)

    The entire function can be shrunk down to this, by the way:

    Thread.Sleep( TimeSpan.FromSeconds( seconds ) );
    

    sigh

  • (cs) in reply to RON

    This guy doesn't know .NET. This guy isn't a developer. What seperates him from the other people we mock here on this site is that this guy is not pretending to be something he's not. I think you're wrong to call him an idiot.

    EDIT: That's wierd. I could swear I hit the quote button.

  • (cs) in reply to ssprencel
    ssprencel:
    This guy doesn't know .NET. This guy isn't a developer. What seperates him from the other people we mock here on this site is that this guy is not pretending to be something he's not. I think you're wrong to call him an idiot.

    Agreed. When we're forced to work with systems we do not understand, the most admirable action of all is to do the most we can with what we have. This guy did just that.

  • df5 (unregistered)

    I concur, he his not a programmer, no WTF.

    Just to be fair, I asked my wife, an english major, to write a program to make the computer pause for a few seconds. She said "Just start OpenOffice." <bada bing!>

  • Cheatz (unregistered)

    Processors are bought with a purpose: Put them to work! Use those cycles!

  • TheMagic8Ball (unregistered) in reply to vt_mruhlin
    vt_mruhlin:
    Not knowing .NET that well, things like this confuse me: Convert.ToDateTime(DateTime.Now.TimeOfDay.ToString());

    It seems intuitive that DateTime.Now.TimeOfDay might already be a DateTime object, so converting it to a String, then back to a DateTime..... ?

    Actually, TimeOfDay is a TimeSpan (since midnight). Since only the time is taken into account (and not the time AND date), it's possible to pause for an entire day. Granted the box would have to be slow (or slowed during the execution of that code..or if the seconds was 0 or possibly 1). In addition, this would not pause at all if the start time is before midnight and the end time is after midnight. It would just exit, in this case.

  • SomeCoder (unregistered)

    The real WTF is that they insisted on moving this code from Perl to .NET.

    I think this is a pretty good effort by someone who doesn't code for a living.

  • Kinglink (unregistered)

    No, the real WTF is the people here thinking this is ok.

    To explain why it is not ok, this code will use as much of the cpu cycles as it can, Thread.sleep uses minimal cycles. Unless you're running a system with out any other programs running on it. Thread.sleep is the only answer. This is a newbie mistake but it's a pretty major mistake.

    Note: while (current_time > Time_to_sleep_to); where current time is a function call to get the current time in milliseconds is ALSO very wrong.

  • (cs) in reply to RON
    RON:
    The entire function can be shrunk down to this, by the way:
    Thread.Sleep( TimeSpan.FromSeconds( seconds ) );
    

    sigh

    Or

    System.Threading.Thread.Sleep(seconds * 1000);

    Why bother instantiating a TimeSpan object?

  • (cs) in reply to Kinglink
    Kinglink:
    No, the real WTF is the people here thinking this is ok.

    To explain why it is not ok, this code will use as much of the cpu cycles as it can, Thread.sleep uses minimal cycles. Unless you're running a system with out any other programs running on it. Thread.sleep is the only answer. This is a newbie mistake but it's a pretty major mistake.

    Note: while (current_time > Time_to_sleep_to); where current time is a function call to get the current time in milliseconds is ALSO very wrong.

    No one said the code is ok. We are giving the author a pass because he is not expected to know .NET. He's a sysadmin, not a programmer. In other words, get off his back. :)

  • cheezedawg (unregistered) in reply to RON
    RON:
    What I think is wrong: snip... 3) Using a 100% CPU utilization loop to "pause" the system. Hooray. (idiot)
    There are some very legitimate reasons to do it this way. I was working on some code a while back that had some tight timing requirements (I needed to sleep for 10-20 microseconds). There are several functions that appear to handle this on the surface (usleep, nanosleep, etc), but the problem is that if you are not using a real-time OS, they all yield to the scheduler. In the case of Linux, the scheduler operates on a 10ms timer, so the minimum that you can really sleep using usleep or nanosleep is 10ms. This was unacceptable for my application.

    I ended up making a similar function to loop until the current time had reached the desired interval, albeit without the unnecessary conversion to a string. There still could be a context switch in the middle of the loop, but it was a lot better than using a sleep function.

  • City Developer (unregistered)

    I think this is a relatively slight WTF on the developer's part - perhaps a WTH? or according to the new title "not quite as bad as failure"?

    Its clear the "developer" here knew what what he knew (and I sense knew it well) and was asked to implement something important in a totally new framework / language / paradigm (which is of course the major WTF here). Given that, he did well to manage to implement something that compiled and did the job. But it doesn't excuse him not stopping and thinking "does what I want to do exist already either as a library function or a code snippet on the web".

    I would suggest that should be every programmer's instinctive natural reaction when asked to implement something which "seems" as though its common and often-used - the chap here should have realised he is unlikely to be the first person to want to do what he had been asked to do, and should have therefore first checked if a CLR function existed to help him, and if it didnt, to see on the web if someone had submitted an existing implementation.

    To do neither and instead to march off and write his own implementation on a totally new framework is a (relatively minor) WTF.

  • (cs)

    C'mon, asking a sysadmin to switch all of his maintenance scripts from stuff that he very well may have just brough with him from job to job to a new language is pretty dumb.

    The non-CodeSOD version of this story would be a post talking about how company Z mandated standardizing on .NET, even the maintenance scripts that controlled the backup validation.

  • (cs) in reply to City Developer
    City Developer:
    To do neither and instead to march off and write his own implementation on a totally new framework is a (relatively minor) WTF.

    If s/he's like any good sysadmin I've ever met, they're too busy supporting too many servers and trying to do too many things. They don't have time to learn the ins and outs of a new framework, they're too busy figuring out how to administer the thing the new framework runs on top of.

  • (cs) in reply to ssprencel
    ssprencel:
    The management or development team should have worked with him to find out what it was he was trying to do and help him. At the least they should have reviewed his code and gave him some pointers.

    No! No! No! This is .NET, not C. Show him the manuals and give him some books. Thus giving him references.

    (sorry :-)

  • (cs) in reply to Cheatz
    Cheatz:
    Processors are bought with a purpose: Put them to work! Use those cycles!

    Q: Why are you running {Seti, or whatever} on this box?

    A: Idle cycles never come back.

  • Anonymous (unregistered) in reply to ssprencel
    ssprencel:
    Kinglink:
    No, the real WTF is the people here thinking this is ok.

    To explain why it is not ok, this code will use as much of the cpu cycles as it can, Thread.sleep uses minimal cycles. Unless you're running a system with out any other programs running on it. Thread.sleep is the only answer. This is a newbie mistake but it's a pretty major mistake.

    Note: while (current_time > Time_to_sleep_to); where current time is a function call to get the current time in milliseconds is ALSO very wrong.

    No one said the code is ok. We are giving the author a pass because he is not expected to know .NET. He's a sysadmin, not a programmer. In other words, get off his back. :)

    Sorry, but it's still poor for a sysadmin. Any decent windows sysadmin would know there are umpteen pre-built sleep implementations already available. Microsoft has various server resource kits that come with a variety of handy tools, one of which is sleep. The popular UnixUtils package is a set of unix tools ported to native Win32... one of which is sleep. And (as others have pointed out) Perl itself is also available for Windows platforms in numerous guises.

  • James (unregistered)

    The real WTF hasn't happened yet -- it will show up in a few years when the next build magically stops behaving as expected, and after a solid month of troubleshooting somebody discovered that the latest Visual Studio service pack made the compiler smart enough to optimize his spin-lock out of existence.

    People, there's a reason we're given a "right" way to do things -- find it and use it!

  • non-judgemental (unregistered) in reply to Anonymous

    But wait, the cpu needs to have its purpose in life fulfilled; to leave cpu cycles unused is somehow - harmful to the universe.

    Seriously, the guy was forced to work with a tool with which he had little or no familiarity. Assuming he was given some sort of deadline (who isn't?), it's probably fair to assume he didn't have much time to go exploring to look up how stuff should be done, or figuring out existing system calls.

    We expect programmers to know to look up stuff in code libraries. This guy wasn't a programmer. A management WTF, maybe.

  • ahem (unregistered) in reply to Tom

    If you make a few substitutions in the story, you can see that this is clearly a management WTF of assigning the wrong person to a task, without the proper training: Reporting from the field, Zak tells us the sad story of a podiatrist forced out of his comfort zone. When you need open heart sugury and your company decides not to hire the correct doctor, the job of performing the operation will almost certainly fall to none other than the podiatrist. This will be the case even if said podiatrist doesn't know cardiac surgury.

    When this happens, you will learn two things. The first is that the new heart will be structured as though it were a foot, even where inappropriate. The second is that some body parts that the podiatrist is used to will be unavailable, or simply not appropriate for the task at hand

  • (cs) in reply to ahem
    ahem:
    If you make a few substitutions in the story, you can see that this is clearly a management WTF of assigning the wrong person to a task, without the proper training: Reporting from the field, Zak tells us the sad story of a podiatrist forced out of his comfort zone. When you need open heart sugury and your company decides not to hire the correct doctor, the job of performing the operation will almost certainly fall to none other than the podiatrist. This will be the case even if said podiatrist doesn't know cardiac surgury.

    When this happens, you will learn two things. The first is that the new heart will be structured as though it were a foot, even where inappropriate. The second is that some body parts that the podiatrist is used to will be unavailable, or simply not appropriate for the task at hand

    Brilliant. Just brilliant. I don't mean that in a Paula kind of way either.

  • Rich (unregistered) in reply to Anonymous

    Agreed. There's no way that he should have at least done a quick search on "C# sleep", which brings back a number of results that very plainly show to use Thread.Sleep

    Regardless of your level of competency, there's no excuse for not doing really basic research.

  • (cs) in reply to Tom
    Tom:
    I think the wtf here would have to be converting server scripts to .NET.

    Honestly, if perl is the best tool for the job, why re-write the scripts?

    Company policy, meddling of eager-beaver consultants, MS-crazy management ... ( many more reasons I'm too lazy to list )

  • PS (unregistered) in reply to Sid2K7
    Sid2K7:
    On another note: What happened to TheDailyWTF? Is this the same site? Granted, I haven't been here for a while, but replacing something as "The Daily WTF" (which has a very clear meaning) with the phrase "Worse Than Failure" is a step in the wrong direction.

    Then, again, the stories seem to be just as hilarious, so ill be back after my usual interval (1-2 months)

    The irony is that you liked the name "The Daily WTF" but then you only visit the site every 1 or 2 months.

  • City Developer (unregistered) in reply to PS
    PS:
    Sid2K7:
    On another note: What happened to TheDailyWTF? Is this the same site? Granted, I haven't been here for a while, but replacing something as "The Daily WTF" (which has a very clear meaning) with the phrase "Worse Than Failure" is a step in the wrong direction.

    Then, again, the stories seem to be just as hilarious, so ill be back after my usual interval (1-2 months)

    The irony is that you liked the name "The Daily WTF" but then you only visit the site every 1 or 2 months.

    I like the Daily Telegraph here in the UK but I only buy it on Mondays (for the Sport) and Friday (for the Crossword)

  • Whitey (unregistered) in reply to RON

    "3) Using a 100% CPU utilization loop to "pause" the system. Hooray. (idiot) "

    To me, this is the real WTF. Lets ask the system repeatedly: what time is it?convert to double, compare.. what time is it?convert to double, compare.. what time is it?convert to double, compare.. what time is it?convert to double, compare..

    Remember people, your computer will do everything you ask, as fast as it can, no matter how dumb it is...

    captcha: ninjas ...cool...

  • Sam (unregistered) in reply to ahem
    If you make a few substitutions in the story, you can see that this is clearly a management WTF of assigning the wrong person to a task, without the proper training:

    Reporting from the field, Zak tells us the sad story of a podiatrist forced out of his comfort zone. When you need open heart sugury and your company decides not to hire the correct doctor, the job of performing the operation will almost certainly fall to none other than the podiatrist. This will be the case even if said podiatrist doesn't know cardiac surgury.

    This is a terrible analogy. Writing a system administration script is not akin open heart surgery. A better analogy would be asking a podiatrist to do a routine suture and finding the patient dead of an infection two weeks later.

    Part of being a system administrator is writing scripts to manage the system. Don't believe me? Look at the SAGE job descriptions for a Junior System Administrator at http://www.sage.org/field/jobs-descriptions.html Note where it says, "Ability to write scripts in some administrative language."

    This is, quite frankly, a stupid script. It would have been a stupid script had it been written similarly in Perl. The correct function to call, "sleep," is the same in both Perl and .NET. And a search on Google for "sleep vb.net" brings up a code sample for the first link, just as doing a search for "sleep perl" brings up the definition of the sleep function in Perl! Finally, after running this script the sysadmin should have seen the CPU peg and realized that it was a stupid script.

    I would hope this is a junior sysadmin that was suitably educated in how to use library functions and Google.

    Of course, I'm curious as to why there was a requirement to move all system administration scripts from Perl to .NET in the first place. Why change a working a system?

  • Whitey (unregistered) in reply to RON

    "3) Using a 100% CPU utilization loop to "pause" the system. Hooray. (idiot) "

    To me, this is the real WTF. Lets ask the system repeatedly: what time is it?convert to double, compare.. what time is it?convert to double, compare.. what time is it?convert to double, compare.. what time is it?convert to double, compare..

    Remember people, your computer will do everything you ask, as fast as it can, no matter how dumb it is...

    captcha: ninjas ...cool...

  • (cs)

    I hope the CPU doesn't melt running like that!

  • Natrone (unregistered) in reply to Jimmy

    Good thing VB doesn't stand for Vagina Bitchslap, or you would have to go through the pain of renaming it to something weak.

  • RON (unregistered) in reply to Mio
    Mio:
    RON:
    The entire function can be shrunk down to this, by the way:
    Thread.Sleep( TimeSpan.FromSeconds( seconds ) );
    

    sigh

    Or

    System.Threading.Thread.Sleep(seconds * 1000);

    Why bother instantiating a TimeSpan object?

    Because

    A) it's more readable B) it doesn't require you to memorize the fact that Sleep requires milliseconds C) If you're concerned about micro-optimizing a call that will subsequently put the system to sleep for a long time, your priorities are out of whack, D) and finally, it's a structure type whose only internal value is a single 64-bit integer, and the act of instantiating it is roughly the same amount of work as multiplying a time value by a magic number (1000).

  • RON (unregistered) in reply to ssprencel
    ssprencel:
    Kinglink:
    No, the real WTF is the people here thinking this is ok.

    To explain why it is not ok, this code will use as much of the cpu cycles as it can, Thread.sleep uses minimal cycles. Unless you're running a system with out any other programs running on it. Thread.sleep is the only answer. This is a newbie mistake but it's a pretty major mistake.

    Note: while (current_time > Time_to_sleep_to); where current time is a function call to get the current time in milliseconds is ALSO very wrong.

    No one said the code is ok. We are giving the author a pass because he is not expected to know .NET. He's a sysadmin, not a programmer. In other words, get off his back. :)

    Normally, when I'm required to work on a system that I have no familiarity with, I ask people who DO know the system, and if I can't find any of those, I use google.

    There is no excuse for "but I didn't know!".

    The term "C# Pause" in google returned a working solution in the very first link.

  • (cs)

    Ok, so the management shouldn't have asked someone who knew nothing about .NET to start utilizing it without giving them training or earmarked time for learning the environment...

    and the sysadmin should have taken a second to google 'C# sleep'...the first result is: http://www.thescripts.com/forum/thread225748.html which answers the question any decent programmer should have asked (which, if he's writing code for the company he should be): "Can I use a sleep command in c#?"

    All in all a minor wtf and not one in the code at all (which is what Code Snippet of the Day is supposed to be about!) but, rather, in the story.

    This entry is a sad one.

  • Joseph Newton (unregistered)

    So, where did this sysadmin learn Perl? Who did he network with? I can't imagine anyone coming from the Perl culture and using that much CamelBack in his code. though the language itself does not place any restrictions on case, fellow Perlites will usually give one a heads-up. The standard--if Perl can be said to have any standard--is that camelback should be reserved for package names.

    And yes...Perl does have a sleep function in its core library.

    [Well, podna', we gots a right friendly captcha today...]

  • Patrick (unregistered) in reply to Will Perdikakis
    Will Perdikakis:
    The code is actually pretty resourceful not a WTF, really.

    He knew what he wanted to do and rather than using the build-in thread function, he used a spin-lock. Lots more wasted CPU cycles, but virtually the same end results.

    How many times have you implemented something that already existed in a standard function library only to find out later?

    Every day.

Leave a comment on “Getting Better Sleep”

Log In or post as a guest

Replying to comment #:

« Return to Article