www.digitalmars.com         C & C++   DMDScript  

D - timer

reply "Carlos Santander B." <carlos8294 msn.com> writes:
Hi,
I'm writing a distributed application where the clients won't have an
interface, they'll only run in background mode. When the server is not
connected, the clients have to try to find it every 5 minutes. How can I do
that? I mean, I know there're the timer functions in the Windows API
(SetTimer, etc.), but they need a window handle, which obviously I won't
have. Any help would be greatly appreciated.

-------------------------
Carlos Santander


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.465 / Virus Database: 263 - Release Date: 2003-03-25
Mar 27 2003
next sibling parent "Carlos Santander B." <carlos8294 msn.com> writes:
"Carlos Santander B." <carlos8294 msn.com> escribiσ en el mensaje
news:b5vb3t$1vm6$1 digitaldaemon.com...
| Hi,
| I'm writing a distributed application where the clients won't have an
| interface, they'll only run in background mode. When the server is not
| connected, the clients have to try to find it every 5 minutes. How can I
do
| that? I mean, I know there're the timer functions in the Windows API
| (SetTimer, etc.), but they need a window handle, which obviously I won't
| have. Any help would be greatly appreciated.
|
| -------------------------
| Carlos Santander
|
|
| ---
| Outgoing mail is certified Virus Free.
| Checked by AVG anti-virus system (http://www.grisoft.com).
| Version: 6.0.465 / Virus Database: 263 - Release Date: 2003-03-25
|
|

Ok, no need to bother anymore. I already got this. MSDN is more powerful
than you'd expect... Thanks anyway.

—————————————————————————
Carlos Santander


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.465 / Virus Database: 263 - Release Date: 2003-03-25
Mar 27 2003
prev sibling parent reply "Matthew Wilson" <dmd synesis.com.au> writes:
If you're on an NT-family box exclusively you can use timer objects - see
CreateWaitableTimer()

If you're on either 9x or NT, then you would have to have a worker thread
waiting for your timeout period on a kernel object - e.g. a mutex or an
event - that was never signalled. Each time it times out, you have your 5
min wait. The worker thread would signal the main thread by sending a thread
message, or window message, or setting an event, or whatever. This setup can
work well when you want to terminate cleanly, as you can use the signalling
of the timeout object as the worker termination command.

I'd be very keen to see your work on either of these things.

On a more general note, can anyone inform on what support D has for thread
creation and synchronisation?

Matthew

"Carlos Santander B." <carlos8294 msn.com> wrote in message
news:b5vb3t$1vm6$1 digitaldaemon.com...
 Hi,
 I'm writing a distributed application where the clients won't have an
 interface, they'll only run in background mode. When the server is not
 connected, the clients have to try to find it every 5 minutes. How can I

 that? I mean, I know there're the timer functions in the Windows API
 (SetTimer, etc.), but they need a window handle, which obviously I won't
 have. Any help would be greatly appreciated.

 -------------------------
 Carlos Santander


 ---
 Outgoing mail is certified Virus Free.
 Checked by AVG anti-virus system (http://www.grisoft.com).
 Version: 6.0.465 / Virus Database: 263 - Release Date: 2003-03-25

Mar 27 2003
next sibling parent reply "Carlos Santander B." <carlos8294 msn.com> writes:
"Matthew Wilson" <dmd synesis.com.au> escribiσ en el mensaje
news:b5vrkv$2edp$1 digitaldaemon.com...
| If you're on an NT-family box exclusively you can use timer objects - see
| CreateWaitableTimer()
|
| If you're on either 9x or NT, then you would have to have a worker thread
| waiting for your timeout period on a kernel object - e.g. a mutex or an
| event - that was never signalled. Each time it times out, you have your 5
| min wait. The worker thread would signal the main thread by sending a
thread
| message, or window message, or setting an event, or whatever. This setup
can
| work well when you want to terminate cleanly, as you can use the
signalling
| of the timeout object as the worker termination command.
|
| I'd be very keen to see your work on either of these things.
|
| On a more general note, can anyone inform on what support D has for thread
| creation and synchronisation?
|
| Matthew
|
| "Carlos Santander B." <carlos8294 msn.com> wrote in message
| news:b5vb3t$1vm6$1 digitaldaemon.com...
| > Hi,
| > I'm writing a distributed application where the clients won't have an
| > interface, they'll only run in background mode. When the server is not
| > connected, the clients have to try to find it every 5 minutes. How can I
| do
| > that? I mean, I know there're the timer functions in the Windows API
| > (SetTimer, etc.), but they need a window handle, which obviously I won't
| > have. Any help would be greatly appreciated.
| >
| > -------------------------
| > Carlos Santander
| >
| >
| > ---
| > Outgoing mail is certified Virus Free.
| > Checked by AVG anti-virus system (http://www.grisoft.com).
| > Version: 6.0.465 / Virus Database: 263 - Release Date: 2003-03-25
| >
| >
|
|

In fact, I went for an easier way (IMHO, at least):

void wait() {
    MSG msg;
    SetTimer(NULL,0,300000,(TIMERPROC) MyTimerProc);
    while (GetMessageA(&msg,NULL,NULL,NULL)) {
        if (msg.message == WM_TIMER) msg.hwnd = NULL;
        TranslateMessage(&msg);
        DispatchMessageA(&msg);
        if (info.sock!==null) {
            KillTimer(NULL,0);
            return;
        }
    }
}
void mtp(HWND hwnd,UINT message,UINT idTimer,DWORD dwTime) { ... }

My first choice was to put mtp() inside wait(), and it actually worked. But
mtp() calls another function, which is outside wait() and it wasn't being
called for some reason that I don't know about. That's why I had to put it
outside.
From a professional POV, is my solution better or worse than those that you
suggested me? Because I just have no idea...

—————————————————————————
Carlos Santander


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.465 / Virus Database: 263 - Release Date: 2003-03-25
Mar 27 2003
parent "Jon Allen" <jallen minotstateu.edu> writes:
SetTimer is the solution I usually use, just because it's easy.  That being
said I've heard that using a second thread as a timer is is more accurate.
I don't know on what basis that claim is made, or even if it's true or not
though.  I'm guessing it has something to do with legacy code in 9x not
updating the timer chip as often as it could, leaving us with a resolution
of only 55ms.  I'm not sure how threads would get around this though.  Maybe
something in the WaitForObjects functions does something really neat?

"Carlos Santander B." <carlos8294 msn.com> wrote in message
news:b5vslt$2feo$1 digitaldaemon.com...
 "Matthew Wilson" <dmd synesis.com.au> escribiσ en el mensaje
 news:b5vrkv$2edp$1 digitaldaemon.com...
 | If you're on an NT-family box exclusively you can use timer objects -

 | CreateWaitableTimer()
 |
 | If you're on either 9x or NT, then you would have to have a worker

 | waiting for your timeout period on a kernel object - e.g. a mutex or an
 | event - that was never signalled. Each time it times out, you have your

 | min wait. The worker thread would signal the main thread by sending a
 thread
 | message, or window message, or setting an event, or whatever. This setup
 can
 | work well when you want to terminate cleanly, as you can use the
 signalling
 | of the timeout object as the worker termination command.
 |
 | I'd be very keen to see your work on either of these things.
 |
 | On a more general note, can anyone inform on what support D has for

 | creation and synchronisation?
 |
 | Matthew
 |
 | "Carlos Santander B." <carlos8294 msn.com> wrote in message
 | news:b5vb3t$1vm6$1 digitaldaemon.com...
 | > Hi,
 | > I'm writing a distributed application where the clients won't have an
 | > interface, they'll only run in background mode. When the server is not
 | > connected, the clients have to try to find it every 5 minutes. How can

 | do
 | > that? I mean, I know there're the timer functions in the Windows API
 | > (SetTimer, etc.), but they need a window handle, which obviously I

 | > have. Any help would be greatly appreciated.
 | >
 | > -------------------------
 | > Carlos Santander
 | >
 | >
 | > ---
 | > Outgoing mail is certified Virus Free.
 | > Checked by AVG anti-virus system (http://www.grisoft.com).
 | > Version: 6.0.465 / Virus Database: 263 - Release Date: 2003-03-25
 | >
 | >
 |
 |

 In fact, I went for an easier way (IMHO, at least):

 void wait() {
     MSG msg;
     SetTimer(NULL,0,300000,(TIMERPROC) MyTimerProc);
     while (GetMessageA(&msg,NULL,NULL,NULL)) {
         if (msg.message == WM_TIMER) msg.hwnd = NULL;
         TranslateMessage(&msg);
         DispatchMessageA(&msg);
         if (info.sock!==null) {
             KillTimer(NULL,0);
             return;
         }
     }
 }
 void mtp(HWND hwnd,UINT message,UINT idTimer,DWORD dwTime) { ... }

 My first choice was to put mtp() inside wait(), and it actually worked.

 mtp() calls another function, which is outside wait() and it wasn't being
 called for some reason that I don't know about. That's why I had to put it
 outside.
 From a professional POV, is my solution better or worse than those that

 suggested me? Because I just have no idea...

 -------------------------
 Carlos Santander


 ---
 Outgoing mail is certified Virus Free.
 Checked by AVG anti-virus system (http://www.grisoft.com).
 Version: 6.0.465 / Virus Database: 263 - Release Date: 2003-03-25

Mar 27 2003
prev sibling parent J C Calvarese <jcc-47 excite.com> writes:
Matthew Wilson wrote:
...snip
 On a more general note, can anyone inform on what support D has for thread
 creation and synchronisation?

Phobos has a "thread" module (which in turn has a Thread class). I'm guessing this is what you're looking for. http://www.digitalmars.com/d/phobos.html#thread Justin
Mar 27 2003