views:

513

answers:

2

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?

A: 

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.

Bob Moore
Thanks, this might be a good solution, however I think I didn't explain the problem correctly. I can't use callback functions. timeSetEvent() uses a callback to perform whatever it needs to be performed.I'm adding a proper explanation to the question.
wonderer
well, that's the way it is. I can't use callbacks.
wonderer
Read the help again, timeSetEvent does NOT have to use a callback. You can specify TIME_CALLBACK_EVENT_SET to set an event. However, if you're not even prepared to create another thread to wait on that event, I don't see how we can help you.
Bob Moore
all i read and the examples I've seen call a callback function.if you can post code it'll be helpful. That's why I wrote that in the question "code is appreciated".
wonderer
A: 

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);
}
PiotrLegnica
true, but my main application is already terminated. the background process is left behind to deal with unfinished db connections.
wonderer
I've edited my answer.
PiotrLegnica
only problems is that the main thread needs to return the status code, then cleanup and then check if needs to terminate. If I put a sleep() it will freeze the application. and as I mentioned several times, I cannot use callback functions. no ThreadProc...
wonderer
thanks for the edit
wonderer
Well, you cannot create a thread without using ThreadProc (this one should be in this background process). Sleep in separate thread won't freeze the application (this thread will be terminated, if main finishes first), and ExitProcess takes care of returning status code (unless you mean something else by "status code").
PiotrLegnica
ok, that's a possible answer: it's not possible.I'll wait until tomorrow. if no there is no other answer then the "not possible" will be the answer and i'll accept yours. Thanks!
wonderer
I'm accepting this as the answer because nothing else is a solution
wonderer