CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Mar 2002
    Location
    Paris
    Posts
    11

    How to use a timer in a win32 dll ?

    Hi,

    I'm using a client/server application from a software company .
    This software allows me to interact with it thanks to a user DLL that I have to implement on the NT side of the communication. This is what they call a "user exit".
    So far so good : each time the software sends or receives a buffer, it fires my DLL, pass information to it , retrieves back updated data, and everything's fine.

    I have an additional need : I would like to detect in my DLL if inactivity occurs for let's say 10 minutes so that I can take special action. Inactivity means my DLL hasn't been fired for 10 minutes.

    I tried to use SetTimer (callback function) / KillTimer in my DLL but as you can imagine, when I call SetTimer in my DLL and then give control back to the main application, the callback application is never called in case of a time-out .

    Is there a way to solve that issue ?

    Thanks for reading and helping me on this topic.

    Marc

  2. #2
    Join Date
    Aug 2001
    Location
    Stockholm, Sweden
    Posts
    1,664
    I think SetTimer is a bad idea due to the fact it requires pumping of window message etc...

    It's kind of hard to give a general solution (don't know enough about the requirments), but I'll try anyway.

    I see, at least, two solutions. The first one is easy.

    Solution 1:
    Compare the time each time you're called. If last time called is longer than the inactivity period, take actions...

    Solution 2:
    Create a thread that hangs a event object. Let the timeout value in the wait function (WaitForSingleObject or WaitForMulitpleObject) control the inactivity period. If the wait function is timed out, set a global flag. Reset the global flag when you're called.

  3. #3
    Join Date
    Mar 2002
    Location
    Paris
    Posts
    11
    Jonas,
    Thanks for your reply.

    My problem is that I must detect the inactivity period outside my DLL.
    As soon as the time-out is reached, I must shutdown the whole application. I cant' wait for my DLL to be called ...

    I've read somewhere else (can't remember) that a possible solution could be to start a new thread dedicated to the timer management from within the DLL.
    Do you think this is doable ?

    Thanks
    Marc

  4. #4
    Join Date
    Aug 2001
    Location
    Stockholm, Sweden
    Posts
    1,664
    Originally posted by legrillon
    I've read somewhere else (can't remember) that a possible solution could be to start a new thread dedicated to the timer management from within the DLL.
    Do you think this is doable ?
    Yepp, that's basically what my second solution was.

    The only problem with this solution, can be that you (your DLL) don't know the life-time etc... I mean, you need to start the thread at some point. The same goes with thread-cleanup. If the DLL interface (that you implement) specifies init/deinit functions, then it's easy and clean. You can of course always start and stop a thread from within DllMain (should work, but I'm more confident with the init/deinit approach).

    How will you terminate the app?

  5. #5
    Join Date
    Mar 2002
    Location
    Paris
    Posts
    11
    There is such a mess in my head... You're right Jonas, it was basically your 2nd solution. Sorry, I need some rest.

    Regarding the shutdown of the app, in fact, I've just changed my mind and I will handle the time-out differently, I explain you how

    Each time my DLL is called, I can look at the buffer to be sent to the host and I can determine if it is a logon request . Logon should be the first request transmitted to the host and then a normal client-server conversation can take place.

    In case of time-out, I want to force the user to log again before resuming his conversation with the host.

    I think I can do that with your solution:
    - a thread taking care of any time-out
    - a static global variable in my DLL that indicates if the next coming buffer should be a logon request ; this variable is set by the timer thread in case of a time-out
    - if my DLL is not called with a logon request as expected, I set the DLL return code to FALSE, the buffer will not be sent by the application, this a capability given by the application and the user will get an error message. This way , I can force the user to submit a logon request again.

    Jonas, thanks a lot!
    I'll try to code that and I'll let you know.
    Marc

  6. #6
    Join Date
    Mar 2002
    Location
    Paris
    Posts
    11
    Jonas,

    It works!

    My DLL creates an Event Object and set it to Signaled each time it's called by the app.

    I have a thread waiting on that Event object with a specific delay, in an infinite loop.
    When the Event gets signaled, the thread determines if it's a time-out or if it was signaled by the parent DLL.
    In the first case, it does a MessageBox and posts a flag to the DLL to force the user to call the logon screen .
    In the second case, it simply resets the Event object and waits again.

    Thanks a lot Jonas, I've really appreciated your help

    Marc

  7. #7
    Join Date
    Dec 2002
    Posts
    47
    Another option is to use the multimedia timer. At least you wouldn't need to create a separate thread.

    The timer accepts a callback function.

    #include <mmsystem.h>

    MMRESULT timeSetEvent(
    UINT uDelay,
    UINT uResolution,
    LPTIMECALLBACK lpTimeProc,
    DWORD_PTR dwUser,
    UINT fuEvent
    );

    -rick

  8. #8
    Join Date
    Mar 2002
    Location
    Paris
    Posts
    11
    Due to PC problem, I've just read your answer today...
    Thanks for your suggestion, rfmobile. Seems to be even simpler!
    Bye
    Marc

  9. #9
    Join Date
    Sep 2003
    Location
    Forever Gone... For Now...
    Posts
    1,515
    Don't forget about waitable timers (CreateWaitableTimer, etc)...
    Thought for the day/week/month/year:
    Windows System Error 4006:
    Replication with a nonconfigured partner is not allowed.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured