wonderer
wonderer

Reputation: 3557

Create a timer on a different thread - with no callback function (C, Windows)

Is there a way to create a timer (say, to 10 seconds) on a different thread? I mean, I know how to use CreateThread() and I know how to create/use timers. The problem I have is that the new thread cannot receive a callback function.

For those that will inevitably ask "why do you want to do this?" the answer is because i have to do it this way. it is part of a bigger program that can't at this specific part of the code use callback functions. that's all.

Is there any way to achieve this?

code is appreciated.

Thanks!

EDIT:

A better explanation of the problem: My application consist of two separate programs. The main program (visible, interface for the user) and another doing the hard work in the background (sort of like a daemon). The background process need to finishing writing to the DB and closing a lot of little files before exiting. The main application send a "we're done" message to that background process. Upon receiving this the background process returns the current status and exists. Now, I need to add the following: upon receiving the message it returns a status and triggers a timer that will wait X amount of time on another thread, in the meantime the background process closes all the DB connections and files. If the timer reached 0 then and the background process is still alive then it terminates it. If the background process closed all the db and files then the thread (and timer) will die before reaching 0 as the application terminates normally.

Is this better?

Upvotes: 1

Views: 1729

Answers (2)

Cat Plus Plus
Cat Plus Plus

Reputation: 129754

So, you need a watchdog inside the DB process (I misread again, didn't I). ThreadProc like this will probably suffice, since all threads terminates when main thread terminates:

DWORD WINAPI TerminateAfter10s(LPVOID param) {
 Sleep(10000);
 ExitProcess(0);
}

Upvotes: 1

Bob Moore
Bob Moore

Reputation: 6894

If you use the multimedia timer function timeSetEvent, it can be configured to pulse an event rather than use the normal callback. Does that satisfy the requirement ?

I'm more interested in knowing why you have this requirement to avoid the use of a callback. Callbacks would seem to be entirely appropriate to use in a worker thread.

Upvotes: 0

Related Questions