Ideas for threading / timer calls / stack

I need to call a method every so often (One for each monitor). That needs to be able to then *timestamp* the object.

I am unsure how to approach this. Last time I succeeded but the problem was it would hang on a while loop (No message handling)so when I created the GUI it would crash. So I am asking for ideas on how I might be able to get it to work.

Class: SingleMonitorInfo

1
2
3
4
5
int pollMonitorPixels()
RETURNS: int
(>0) method failed
( 0 ) method succeeded, stream is within limits, stream not edited or edited within limits
( 1 ) method succeeded, stream is outside limits, stream replaced


So my pseudo-code looks something like this:


Method 1 - Set time stamps for all monitors
1
2
3
4
5
6
7
// Get called after (x) seconds
// For all monitors
//     If monitor is not set to active
//     switch (pollMonitorPixels())
//         >0 - Error handeling
//         0 - Don't set time stamp
//         1 - Set time stamp to current time 



Method 2 - Checks if time stamps are over (x) seconds / minutes / hours

1
2
3
4
// Get called after Method 1
// For all monitors
//     If monitor (time stamp - current time) is equal to or greater than (x)
//         runMethodX 



I am very much a beginner. Currently I am thinking of creating a new class with simple multi threaded queue handler, but unsure where to begin. Any help would be much appreciated.
Last edited on
Why don't you use a Timer ? In the WM_TIMER you can call Method 1 and Method 2. Since you are a beginner a multi threaded queue handler seems to complicated.
That is the other option I was considering, thanks.

What worries me about WM_TIMER is the inaccuracy of when it will get processed.

...However, for more accurate time measurement (elapsed time less than 1 sec), these timers are hardly the solution. The main reason is that timer posts WM_TIMER messages to a message queue, and we can never be sure when this message will be processed. Now, you might think that setting lpTimerFunc is a solution to this problem, but that is not the case. If you specify lpTimerFunc, the default window procedure calls it only when it processes WM_TIMER. So, we will still wait for WM_TIMER to be processed.
Last edited on
How accurate does it have to be ?
You could also integrate this into the message loop by checking the interval with a function like GetTickCount()...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
WAIT_TIME_MS = 2000

while (GetMessage(...))
{
    if (GetTickCount() > lasttick + WAIT_TIME_MS)
    {
        func1();
        func2();
        lasttick = GetTickCount();
    }
    else
    {
        TranslateMessage();
        DispatchMessage();
    }
}

(obviously not real code)
Topic archived. No new replies allowed.