Solved

Create and Receive Timer Event in WIN32 DLL

Posted on 2002-03-20
8
585 Views
Last Modified: 2012-06-27
Dear Experts:

I would like to know if there is any ways to create a Timer say 500ms and a Timer Event Handler in a traditional WIN32 DLL??

If sample code can provide will be even better.

Thanks in advance

Viper Venom
0
Comment
Question by:viper_gts
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 6882079
MSDN Says

Creating a Timer
The following example uses the SetTimer function to create two timers. The first timer is set for every 10 seconds, the second for every 5 minutes.

// Set two timers.
 
SetTimer(hwnd,             // handle to main window
    IDT_TIMER1,            // timer identifier
    10000,                 // 10-second interval
    (TIMERPROC) NULL);     // no timer callback
 
SetTimer(hwnd,             // handle to main window
    IDT_TIMER2,            // timer identifier
    300000,                // 5-minute interval
    (TIMERPROC) NULL);     // no timer callback
To process the WM_TIMER messages generated by these timers, add a WM_TIMER case statement to the window procedure for the hwnd parameter.

case WM_TIMER:
 
    switch (wParam)
    {
        case IDT_TIMER1:
            // Process the 10-second timer.
 
             return 0;
 
        case IDT_TIMER2:
            // Process the 5-minute timer.

            return 0;
    }
An application can also create a timer whose WM_TIMER messages are processed not by the main window procedure but by an application-defined callback function, as in the following code sample, which creates a timer and uses the callback function MyTimerProc to process the timer's WM_TIMER messages.

// Set the timer.
 
SetTimer(hwnd,                // handle to main window
    IDT_TIMER3,               // timer identifier
    5000,                     // 5-second interval
    (TIMERPROC) MyTimerProc); // timer callback
The calling convention for MyTimerProc must be based on the TimerProc callback function.

If your application creates a timer without specifying a window handle, your application must monitor the message queue for WM_TIMER messages and dispatch them to the appropriate window. Note that GetMessage can return -1 if there is an error.

HWND hwndTimer;   // handle to window for timer messages
MSG msg;          // message structure
 
    while (GetMessage(&msg, // message structure
            NULL,           // handle to window to receive the message
            NULL,           // lowest message to examine
            NULL)          // highest message to examine
           != 0 && GetMessage(&msg, NULL, NULL, NULL) != -1)
    {
 
        // Post WM_TIMER messages to the hwndTimer procedure.
 
        if (msg.message == WM_TIMER)
        {
            msg.hwnd = hwndTimer;
        }
 
        TranslateMessage(&msg); // translates virtual-key codes
        DispatchMessage(&msg);  // dispatches message to window
    }

GOOD LUCK
0
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 6882080
MSDN Says

Creating a Timer
The following example uses the SetTimer function to create two timers. The first timer is set for every 10 seconds, the second for every 5 minutes.

// Set two timers.
 
SetTimer(hwnd,             // handle to main window
    IDT_TIMER1,            // timer identifier
    10000,                 // 10-second interval
    (TIMERPROC) NULL);     // no timer callback
 
SetTimer(hwnd,             // handle to main window
    IDT_TIMER2,            // timer identifier
    300000,                // 5-minute interval
    (TIMERPROC) NULL);     // no timer callback
To process the WM_TIMER messages generated by these timers, add a WM_TIMER case statement to the window procedure for the hwnd parameter.

case WM_TIMER:
 
    switch (wParam)
    {
        case IDT_TIMER1:
            // Process the 10-second timer.
 
             return 0;
 
        case IDT_TIMER2:
            // Process the 5-minute timer.

            return 0;
    }
An application can also create a timer whose WM_TIMER messages are processed not by the main window procedure but by an application-defined callback function, as in the following code sample, which creates a timer and uses the callback function MyTimerProc to process the timer's WM_TIMER messages.

// Set the timer.
 
SetTimer(hwnd,                // handle to main window
    IDT_TIMER3,               // timer identifier
    5000,                     // 5-second interval
    (TIMERPROC) MyTimerProc); // timer callback
The calling convention for MyTimerProc must be based on the TimerProc callback function.

If your application creates a timer without specifying a window handle, your application must monitor the message queue for WM_TIMER messages and dispatch them to the appropriate window. Note that GetMessage can return -1 if there is an error.

HWND hwndTimer;   // handle to window for timer messages
MSG msg;          // message structure
 
    while (GetMessage(&msg, // message structure
            NULL,           // handle to window to receive the message
            NULL,           // lowest message to examine
            NULL)          // highest message to examine
           != 0 && GetMessage(&msg, NULL, NULL, NULL) != -1)
    {
 
        // Post WM_TIMER messages to the hwndTimer procedure.
 
        if (msg.message == WM_TIMER)
        {
            msg.hwnd = hwndTimer;
        }
 
        TranslateMessage(&msg); // translates virtual-key codes
        DispatchMessage(&msg);  // dispatches message to window
    }

GOOD LUCK
0
 

Author Comment

by:viper_gts
ID: 6882097
Thank you for such a fast response.

Please correct me if I am wrong but I think in a traditional WIN32 does not have a window own by itself.

Therefore, both way cannot be implement in this case.

Viper Venom
0
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
LVL 8

Accepted Solution

by:
fl0yd earned 50 total points
ID: 6882101
Hi viper_gts,
there is a function
UINT_PTR SetTimer( HWND hWnd,
    UINT_PTR nIDEvent,
    UINT uElapse,
    TIMERPROC lpTimerFunc )
that suits your needs. You can use it either by specifying your own callback-routine or handle the WM_TIMER messages in your main window procedure. In the latter case set
lpTimerFunc = NULL;
Don't forget to link to User32.lib and #include "windows.h".

.f
0
 
LVL 8

Expert Comment

by:fl0yd
ID: 6882109
p.s.: Took me toooooo long to type.....

What exactly do you mean by 'traditional WIN32'? I assume, you're talking about an command-line-application without a GUI. You can still use ::SetTimer(...), as you don't have to specify a window handle. In that case make sure to provide your own callback-function.

.f
0
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 6882113
You can set NULL, for window, So u can set only one timer, But in ur case this may okay... right ?

Roshmon
0
 

Author Comment

by:viper_gts
ID: 6882115
Actually, I am trying to write a small test program and see if it works.

Thank you for all you help.

Viper Venom
0
 
LVL 12

Expert Comment

by:pjknibbs
ID: 6882623
fl0yd: This method will only work if the program which launches the DLL has a message loop. 99% of Windows programs DO have a message loop, of course, but I'm not sure you can guarantee this when the timer is being used in a DLL.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C Language combined operators 28 109
Grammars for C C++ and java 1 122
GUI: DIalog Stacking and Popping in MS C++ 4 76
How to print into std::string like sprintf using C++11? 12 60
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question