Solved

timeout function?

Posted on 1998-11-11
8
342 Views
Last Modified: 2010-04-02
In JavaScript you can use the timeout function to perform an action every 10 seconds or whatever # you want. Is there a similiar way to do this in C++ with Win 3.1?
0
Comment
Question by:goldseal
  • 4
  • 4
8 Comments
 
LVL 2

Accepted Solution

by:
harrys earned 20 total points
ID: 1177645
Use SetTimer() to create the timer. You can setup a callback function every time the timeout is reached or you can just process the WM_TIMER message which is generated
when the timeout is reached.
See the WinAPI reference for more details
0
 

Author Comment

by:goldseal
ID: 1177646
I guess I should have been a little more clearer. The answer is great, but I am just learning C++ so don't know quite how to do this. I have some background with Java and objects, but could you provide a small example that will do function x() after 2 seconds?

thanks
0
 
LVL 2

Expert Comment

by:harrys
ID: 1177647
no problem,
with the plain Windowsapi you would do something like this..

// inside main
   if( !SetTimer( hwnd, 0, 2000, TimerProc ) )
        return (0);

// somewhere
long FAR PASCAL WindowProc( HWND hWnd, UINT message,
                            WPARAM wParam, LPARAM lParam )
{
    switch( message )    {
    case WM_TIMER:
        x();
        break;
    }
}

0
 

Author Comment

by:goldseal
ID: 1177648
Thanks a bunch. A little clarification:

what is TimerProc here?
if( !SetTimer( hwnd, 0, 2000, TimerProc

And, is this just a function for the API?
long FAR PASCAL WindowProc( HWND hWnd, UINT message,
                            WPARAM wParam, LPARAM lParam )

I haven't learned that yet. Just cout with DOS. Do I need that?

thanks a bunch
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 2

Expert Comment

by:harrys
ID: 1177649
The Function Called WindowProc should be called TimerProc instead, it was an error I made, it should be

long FAR PASCAL TimerProc( HWND hWnd, UINT message,
                                   WPARAM wParam, LPARAM lParam )


0
 

Author Comment

by:goldseal
ID: 1177650
Thanks, but what does

long FAR PASCAL TimerProc( HWND hWnd, UINT message,
                                   WPARAM wParam, LPARAM lParam)

all of this mean? All of these parameters and stuff?

thanks again
0
 
LVL 2

Expert Comment

by:harrys
ID: 1177651
Its a Windows API callback function, the adress of the function is passed to windows, when the timer is created in SetTimer(), the declaration is like every standard Windows
callbackfunction, you only need to process the parameter wParam which holds the
ID of the Timer which generated the WM_TIMER message.
The parameter hWnd holds the windowhandle you passed via SetTimer, message is the
typ of message, WM_TIMER in this case.
FAR means that the function is stored in a FAR pointer ,with segment and offset to make shure that the function could be called from outside your code segment.
PASCAL mean that the function uses PASCAL calling conventions, which affects the way
how the compiler generates the code.
This is documented in the Windows API documentation which every windowscompiler
should have "onboard", you might take a look there.
0
 

Author Comment

by:goldseal
ID: 1177652
thanks a bunch
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now