Thread: Timers

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    41

    Timers

    Hi,

    don't you just hate timers? I know I do (sort of ) They're never on time especially if you specify timers lower than approximately 50 ms and that's exactly what I want.
    I know I can use GetPerformanceTimerCount, ect... But I want to create a timer I can leave behind, that will call a function without me checking the timer.
    I've also found something about WaitableTimers, but that's not what I want either, because like I said I want to leave them alone, not call WaitForSingleObjectEx or something.

    This is because I use a dll that sets timers on certain events. And the dll isn't allowed to slow down a thread by calling a wait function or looping to see if it is time yet. It has to give the calling thread back the focus.

    So I thought of creating another thread that handles the timers (probably waitable timers). But how do I do this?
    Because my dll is loaded by a lot of threads and I should only create one thread accessable to the dll (no matter what thread is calling it). I already have a FileMap to share variables over those calling threads, can I also add the thread there or something?

    (btw how could I call a function in the created thread from my dll? or should I create another waitable timer i can set my dll (or some kind of event or queue))

    Hum, I've read this again, and I think I have to read something about threads again, because they're not really clear to me yet. They're run in the same address space as the creating thread, so it could use the same vars? (the creating thread would now be the calling thread of the dll???)
    And about creating only one extra thread. I've thought about it, and I always have at least one thread attached to it, so I could create a thread in the address space of that process. But I'm still a little unsure of how to tell it to create another timer...

    Joren

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    41
    I've tried a little setup for this:
    one dll and one exe.
    The first exe to load the dll creates the filemap and the timer thread.
    The dll sets timerhandles for use with the timer thread.

    This is what I want, but I can't get access to the handles from the other processes that load the dll (other instances of the same exe).
    I've read that you have to use DuplicateHandle to get a copy of a WaitableTimer. But I can't use it, because I don't know which processes will load the dll.
    So I thought I could use the filemap to save the handles of the timers and call duplicatehandle for each attaching process. This doesn't work because I still need a handle to the process that created the timers. And I can't use that handle unless I duplicate it, and there I have the same problem again.
    And I can't communicate between the processes because I don't own them (in this example with multiple instances of the same exe I do, but in my real program I don't).
    How can I use the handles in different processes? (or any handle for that matter...)

    I thought about saving the handle to the creating process in the filemap. But using the handle returned by GetCurrentProcess() is only allowed in the process itself.
    I just read something about OpenProcess() which can create a handle to a process from the processID (which can be stored in a filemap?). Then I can duplicate the handles of the timers stored in the filemap? (I don't think so, because the handles stored in the filemap are still not valid when duplicating in the new process).

    I'm just brainstorming here, if you have an idea, feel free to jump right in

    Joren

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    41
    I think this worked. Although I thought I couldn't duplicate the times because they weren't created by the extra processes, it seems I can. If I only open the process using the processID.
    If anybody knows if this is correct or not (maybe it's very risky or something) please tell me.

    And can I call SetWaitableTimer() while the timer is in a wait function?
    And what about filemaps, I can't let two processes write to the filemap at the same time? But can I read?

    Joren

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. timers in c
    By steve1_rm in forum C Programming
    Replies: 10
    Last Post: 02-24-2009, 12:34 PM
  2. Threads and Timers
    By scioner in forum C Programming
    Replies: 8
    Last Post: 03-22-2008, 07:56 AM
  3. switch cases and timers
    By soranz in forum C++ Programming
    Replies: 5
    Last Post: 10-02-2005, 06:43 PM
  4. Timers, Timers, Timers!
    By Stan100 in forum Game Programming
    Replies: 9
    Last Post: 01-24-2003, 04:45 PM