views:

75

answers:

3

i want to implement a code to keep a watch on suppose some event ...at the meantime i don have any inbuilt eventwatcher so i hv to implement one of mine..which consumes least cpu & memory.

can u suggest me one..

for example a pseudocode is given:

while(true)
{
    if(process.isrunning)
        process.kill();
}
+1  A: 

If you don't have any event to hook into, then your code has to be "active" to run the checks. And that costs CPU cycles.

What you can to do ease waste is to add a call to sleep (Thread.Sleep in .NET, sleep in some implementations of C++).

while (true) {
    if(process.isrunning)
         process.kill();

    sleep(100);   // Wait 100 millisecond before trying again 
}

But that will make you code a little less responsive.

Arve
and if that event happens in that 100 milliseconds!!!!!??????
peril brain
@perilbrain - if you don't have hooks, you'll need to trade off accuracy vs. CPU overhead. Anyway, even if you never sleep, you will risk missing an event if an event appears and disappears when the scheduler is not running your process.
R Samuel Klatchko
A: 

you can try using timer queue : http://msdn.microsoft.com/en-us/library/ms687003%28VS.85%29.aspx its basically using kernel scheduler to call your function callback at specified interval, the caller is from different thread so it won't interrupt the main thread and make your application responsive, the thread is managed by Windows so you don't have to manage your own pooling thread, and its relative accurate.

implementation example: `

//a singleton class that hold timer queue
class TimerQueue {
    protected:
        HANDLE timerQueue;
        TimerQueue() { 
            this->timerQueue = ::CreateTimerQueue(); 
        }
        ~TimerQueue() {
            if(this->timerQueue) {
                ::DeleteTimerQueueEx(this->timerQueue,NULL);
                this->timerQueue = NULL;
            }
        }
    public:     
        static HANDLE getHandle() {
            static TimerQueue timerQueueSingleton;
            return timerQueueSingleton.timerQueue;
        }
}

//timer base class
class Timer
{
protected:
    HANDLE timer;
    virtual void timerProc() = 0;
    static void CALLBACK timerCallback(PVOID param,BOOLEAN timerOrWait) {
        Timer* self = (Timer*)param;
        self->timerProc();
    }
public:
    Timer(DWORD startTimeMs,DWORD periodTimeMs) {       
        if(!::CreateTimerQueueTimer( &this->timer, TimerQueue::getHandle(), 
                                    (WAITORTIMERCALLBACK)&this->timerCallback, 
                                     this, startTimeMs, periodTimeMs,
                                     WT_EXECUTEDEFAULT) ) {
            this->timer = NULL;
        }
    }
    virtual ~Timer() {
        if(this->timer) {
            ::DeleteTimerQueueTimer(TimerQueue::getHandle(),&this->timer,NULL);
            this->timer = NULL;
        }
    }
}

//derive and implement timerProc
class MyTimer : public Timer
{
    protected:
        virtual void timerProc() {
            if(process.isRunning()) {
                process.kill();
            }
        }
    public:
        MyTimer(DWORD startTimeMs,DWORD periodTimeMs) 
            : Timer(startTimeMs,periodTimeMs) {}
}

//usage:
int main(int argc,char* argv[]) {
    MyTimer timer(0,100); //start immediately, at 10 Hz interval
}

`

disclaimer : i don't test or compile those codes, you should recheck it

uray
A: 

Although you've tagged this as language-agnostic, any good implementation is going to vary widely not just from one language to another, but across operating systems. There are plenty of circumstances where programs or operating system functions need to do just this sort of thing, and mechanisms will have been implemented to do this in as sensible, non-intrusive a way as possible.

If you have a particular language and/or operating system in mind, please tell us, and give us a better idea of what you're trying to achieve. That way we can point you towards the most appropriate of the many possible solutions to this problem.

Tim