Problem with Sleep() #$@^#$%^

This is a discussion on Problem with Sleep() #$@^#$%^ within the C++ Programming forums, part of the General Programming Boards category; Hi All, I am using extensive Sleep() function in my application coz i want it to halt at certain point ...

  1. #1
    Registered User intruder's Avatar
    Join Date
    Nov 2002
    Posts
    48

    Unhappy Problem with Sleep() #$@^#$%^

    Hi All,

    I am using extensive Sleep() function in my application coz i want it to halt at certain point of time. But some people told me that Sleep() is machine dependant as my application doesn't show consistant behavior from machine to machine.

    Hence i am trying to use SetTimer() OnTimer() functions to achieve the Sleep() functionality but it ain't happening coz whenever i try to do SetTimer() it executes the OnTimer function independantly as a seperate process and control doesn't stop there.

    pls help me how can i achieve the Sleep() functionality without actually using
    Sleep() function. Is there any other way we can do the same ?

    Thanks in advance.

    in.

  2. #2
    Registered User Rare177's Avatar
    Join Date
    May 2004
    Posts
    214
    one simple way i thought of is SetTimer and a function for the timer, return a value to the function say
    Code:
    return 2;
    then after you use SetTimer call something like
    Code:
    while(1){
    if(TimerFunction == 2){
    continue with whats needed
    break;
    }
    }
    its not really the best way but it will do it
    also dont forget to kill the timer where it would be needed
    Last edited by Rare177; 10-09-2004 at 04:03 AM.
    Good Help Source all round
    Good help for win programmers
    you will probably find something in here
    this thing also helps

    if you have never tried any of the above then maybe you should, they help alot

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    > I am using extensive Sleep() function in my application coz i want it to halt at certain point of time
    You're probably doing something wrong then.

    If you're trying to synchronise with something else then you need events, not Sleep(). Most software should get the job done as fast as possible (ie, not sleeping), then go back to waiting for the next event (say user input) which triggers the next bit of work.

    > But some people told me that Sleep() is machine dependant
    http://msdn.microsoft.com/library/de...base/sleep.asp
    I rather suspect that you'll have the same problem with timers. They only guarantee a minimum time delay, not an exact time delay. This will no doubt vary from machine to machine.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User intruder's Avatar
    Join Date
    Nov 2002
    Posts
    48
    Hey Rare i tried the same way but a little other way. what i have done is i have declared a global bool value and i am initializing it to false initially than making it false when the OnTimer() function is executed but it ain't working .. it hangs my application.
    herez the code for ur reference.


    Code:
    bool g_bFlag = false; //Global variable.

    OnOk() //on ok click event.
    {
    SetTimer(1, 2000, NULL);
    while(1)
    {
    if (g_bFlag)
    {
    break;
    }
    }
    }


    OnTimer(uint nID)
    {
    switch(nID)
    {
    case 1:
    g_bFlag = true;
    KillTimer(1);
    break;
    case 2:
    break;
    }
    }

    i have tried this but my application hangs don't know why ?

    What can b the other option ? or is there any other functions we can use to simulate Sleep().

  5. #5
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    You need to run a message loop if you want to use a SetTimer() windows timer. Sleep() will probably be much more accurate than a timer and accurate enough for most purposes, especially with such a long interval period. Sleep() is usually accurate to within 10ms.

    Could you tell us what you're trying todo? I suspect Sleep() is getting the blame unfairly.

  6. #6
    Registered User intruder's Avatar
    Join Date
    Nov 2002
    Posts
    48
    I have developmed an application which sends a request to hardware and than the hardware responds with a response.
    This response which is recieved is dependant on what request message i am sending to the hardware. For certain type of messages the hardware will send the response after 60 secs. hence after sending the request messsage i have to wait for 60 secs and than only my card will recieve the message from the hardware.

    I am using LON protocol and sending LON requests and the hardware is responding to these requests.
    In many cases the response comes after 30 secs. or 500 millisecs and the response comes and sits in the PCLTA-20 card so my application waits for the specific time and than reads the card for response.

    But i have tested the application many a times but the results what i am finding is inconsistant in same machine as well as diff. machine, so i think Sleep() is machine dependant... more over people told me its like that . but i am doubtful hence trying to do something diff. which will serve the purpose of Sleep().

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    > PCLTA-20 card
    One of these?
    http://www.echelon.com/support/docum...8-0179-01C.pdf

    I rather suspect you need to be polling the card frequently to see if anything interested has happened, then acting on that.
    My suspicion is that with such long intervals, the card is losing information (internal buffer overflow) because your code takes too long to find out what happened.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User intruder's Avatar
    Join Date
    Nov 2002
    Posts
    48
    Hi Salem,

    No that is not the case beause when i am sending the LON request the hardware device to which i am sending the request itself will send the response after pre-specified time.

    The behaviour of the hardware is like that, with the help of LON request i am sending a command to the hardware and the hardware is executing the command. This execution of command takes sometime and after the completion of execution the hardware send back response which my application reads.

    Now the LON card will recieve the message only after the pre-specified time and my application waits for some time which is less than the per-specified time and i start reading the card actually before the hardware sends the response. So in that case i don't think the card might be loseing the information.
    But still am not sure coz the behaviour is not consistant and hence i am doubting on Sleep() function.
    This function is not halting the application for that much time everything and hence many a times the card loses the info. so this can be the case.

    and hence i am trying the simulation of Sleep with the help of something else.
    if u have any solution it wud be very helpful..

    Thanks in advance.

    In.

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    I would suggest you write some test code (just sends one command, waits for one reply) and experiment with various polling and sleeping strategies to figure out what works and what doesn't.

    Is there no sample code which came with the card, or on the manufacturer website?
    Or even go hassle their tech support line.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. sleep() problem when Time Hits Midnight
    By seemarj83 in forum C Programming
    Replies: 4
    Last Post: 05-25-2007, 02:32 AM
  3. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21