CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Hybrid View

  1. #1
    Join Date
    May 2012
    Posts
    26

    Question How do i make i timer to sleep my app from time to time or far some time?

    hi guys...

    i have this code for creating a module that is introduced in another program

    Code:
    #include ".\BoolGui.h"
    #include "windows.h"
    #include <time.h>
    
    REGISTER_GUI_PLUGIN( BoolGui, L"My Bool" );
        
    void wait(long seconds)
    {
    
    seconds = seconds * 3000;
    Sleep(seconds);
    
    }
    
    
    
    BoolGui::BoolGui( IMpUnknown* host ) : MpGuiBase(host)
    {
        wait(3);
    
    }
    i now can sleep my application for 3 sec, but i would like to make it sleep for 3 sec after 10 sec for eg....how can it be done?

    Thank you!

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: How do i make i timer to sleep my app from time to time or far some time?

    The Sleep param is in milliseconds
    Code:
    seconds = seconds * 3000;
    How many milliseconds equals a second?
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  3. #3
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: How do i make i timer to sleep my app from time to time or far some time?

    i now can sleep my application for 3 sec
    A few words on this statement. Sleep affects only current thread where it was called, not "application" in whole. So, in case your plug-in is docked into multi-threaded app, you block the plug-in thread. BTW, this side effect seems not much friendly behavior in aspect of potential blocking of all the other plugins the app may host. You definitely have to consult with app designer whether this behavior is acceptable.

    The more funny part is that plug-in architecture typically expects from the plug-ins be perfectly passive and respond back only been called explicitly. In other words, most of the times the host decides about inactivity schedules, not plug-ins. Of course, exceptions always possible.

    As for your question. Interleaving periods of activity with sleeps will require for special time management thread. Details are gonna depend on the worker thread architecture. In case the thread pumps messages, the manager thread can post custom message on which the thread falls to sleep for 3 sec.

    Another approach would be sending a signal to the worker thread. In its turn the thread should check the signal periodically. Say the signalling object would be an event. Manager thread sets the event when work is allowed (for 10 seconds in your case). Worker thread periodically tries to wait on event object, but as long as the object signals, the wait is not possible. After elapsing 10 seconds the manager resets the event. Due to this the next try to wait will block the worker until manager sets event (after 3 seconds in your case).

    As you may see, there are few points to think over, like required periodical nature of the worker, the period length, possible inaccuracy in sleep intervals, etc.

    CreateEvent
    SetEvent
    ResetEvent
    WaitForSingleObject
    WaitForMultipleObjects
    CreateWaitableTimer

    I hope the idea is clear enough.
    Best regards,
    Igor

  4. #4
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: How do i make i timer to sleep my app from time to time or far some time?

    If you want to do something (including sleep) in N seconds, you can set-up a timer for N seconds, and when the timer elapses perform that action.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  5. #5
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: How do i make i timer to sleep my app from time to time or far some time?

    But beside that point. Other than a few very special cases dealing with external synchonization. Any application that "needs" Waits, or sleeps or delay loops is more likely than not exposing a design/implementation flaw/bug.

  6. #6
    Join Date
    May 2012
    Posts
    26

    Re: How do i make i timer to sleep my app from time to time or far some time?

    thanks for your help guys,

    the thing here is that when this code is performed
    Code:
    BoolGui::BoolGui( IMpUnknown* host ) : MpGuiBase(host)
    {
        wait(3);
    
    }
    my application initialize the module and keep it initialized while is open when i set the "wait" function it realy waits for 3 secs ...(btw seconds = 1000) in first void (i mistake) ...anyway...there is no problem in using the "wait" function where it is..i tried out and it waits for 3 seconds when initialized..what i need is a loop to set here in order to sleep the module wich is a .dll similar from time to time....this application is a sound app and im trying to make a trial version of it by using sleeping the sound from time to time instead of using registry entries.... it works..when im building the app wich runs in a visual programming compiler, each time i insert the module the sound sleeps for 3 seconds and then start again... my problem is making a second timer to set a limit for a testing mode of a trial version...

    can this be done?

  7. #7
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: How do i make i timer to sleep my app from time to time or far some time?

    As Cilu said use timers instead of sleep. See this http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured