-
August 9th, 2012, 06:36 PM
#1
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!
-
August 10th, 2012, 01:12 AM
#2
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?
-
August 10th, 2012, 01:13 AM
#3
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
-
August 10th, 2012, 02:12 AM
#4
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.
-
August 10th, 2012, 04:23 AM
#5
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.
-
August 10th, 2012, 04:24 PM
#6
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?
-
August 10th, 2012, 04:28 PM
#7
Re: How do i make i timer to sleep my app from time to time or far some time?
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|