Solved

timeout function?

Posted on 1998-11-11
8
345 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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
 
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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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 goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

809 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