Hi all,

I am zawpai. I would like to create the timer function in C to do some checking. Could you please show me how to write the code about it.

Thanks,
zawpai

Not sure what you mean. Do you want to create a function that is called by the operating system every few seconds ? That would depend on the operating system. I know its possible in MS-Windows programs but I don't know about other operating systems such as *nix.

If that is not what you want then you might explain in more detail or post some psudocode, such as this.

get start time
  do something
get stop time
show difference between start and stop times

Hello,
Sorry for about it that is not to interpret clearly. Because I want to do a task in every 200 millisecond. It just likes the timer object in VB 6.0. I will show a sample program that is got from some website.

#include <stdio.h>
#include <sys/types.h>
#include <time.h>

main()
{ int i;
  time_t t1,t2;

  (void) time(&t1);
   for (i=1;i<=300;++i) printf("%d %d %d\n",i, i*i, i*i*i);
      
   (void) time(&t2);
   printf("\nTime to do 300 squares and cubes= %d seconds\n", (int) t2-t1);
}

If you run that program, the first result will start from 3. I think it should be started from 2. Is it corrected or not?
Why (t2-t1)=0?

Could you please write some sample code and show to me?

Thanks,
zawpai

If you are in the MS-Windows environment include <windows.h>, call SetTimer() and write your timer event handler.

Example:

#include <windows.h>
#include <iostream>
using namespace std;

#pragma comment(lib,"User32.lib")

VOID CALLBACK TimerProc (HWND hwnd,UINT uMsg,UINT_PTR idEvent,DWORD dwTime)
{
    KillTimer(hwnd,uMsg);
    cout << "timer event called\n";
}


int main()
{
    HWND hWnd = 0;
    hWnd = GetConsoleWindow();
    UINT_PTR t = SetTimer(hWnd,100,200,TimerProc);
    if(t == 0)
    {
        DWORD dwError = GetLastError();
        char buf[255] = {0};
        FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
            0,dwError,0,buf,sizeof(buf),0);
        cout << buf << "\n";
    }
    cin.ignore();
    return 0;
}

>Example:
Now you only have to port it to C.

By the way. I am having troubles making it work in Dev C++.
As a C++ compilation I get this error: `GetConsoleWindow' undeclared (first use this function)
As a C I get the following error: undefined reference to `GetConsoleWindow'

I went to msdn and these are the requirements:

Client Requires Windows Vista, Windows XP, or Windows 2000 Professional.
Server Requires Windows Server 2008, Windows Server 2003, or Windows 2000 Server.
Header

Declared in Wincon.h; include Windows.h.
Library

Use Kernel32.lib.
DLL

Requires Kernel32.dll.

Which brings it to my question.
What should I interpret by Use Kernel32.lib and Requires Kernel32.dll
Do I need to do something to make sure that Kernel32.dll is used by the compiler or it is suppose to be already included by the Windows.h header file?

Comments
who is the chick with the bloodshot lips?

Hi,

I also get the same error like 'Aia' said. How about my sample program? Have you run it?

Could you please explain us clearly ?

Waiting for you reply.

Best Regards,
zawpai

>or it is suppose to be already included by the Windows.h header file?
Headers only contain declarations. To get the definitions you need to link with a library (ie. Kernel32.lib or Kernel32.dll).

Here's a poor man's timer:

#include <stdio.h>
#include <time.h>

void wait ( double seconds )
{
  clock_t start = clock();

  while ( clock() < start + ( seconds * CLOCKS_PER_SEC ) )
    ;
}

/* For symmetry with stop_timer */
clock_t start_timer ( void )
{
  return clock();
}

double stop_timer ( clock_t start )
{
  return ( (double)clock() - start ) / CLOCKS_PER_SEC;
}

int main ( void )
{
  clock_t start;

  puts ( "Running" );
  start = start_timer();
  wait ( 2.2 );
  printf ( "Done after %f seconds\n", stop_timer ( start ) );

  return 0;
}

>Example:
Now you only have to port it to C.

Oops! I used iostreams by mistake.


By the way. I am having troubles making it work in Dev C++.
As a C++ compilation I get this error: `GetConsoleWindow' undeclared (first use this function)

After poking around Dev-C++\include\DDK I see that it is missing quite a few files. You will probably have to download the Windows Platform SDK from M$ and use that instead of the package that is supplied with Dev-C++. GetConsoleWindow() is defined in wincon.h which is not part of Dev-C++ win32 package.

@Narue: Thank you for giving me some explanation.
@Ancient Dragon: Thank you for taking the time of looking into it.

Actually DevC++ has a wincon.h header file with a prototype of GetConsoleWindow(void); inside a:

#if (_WIN32_WINNT >= 0x0500)
BOOL WINAPI GetConsoleDisplayMode(LPDWORD);
HWND WINAPI GetConsoleWindow(void);
#endif

and following the remarks in MSDN

Remarks

To compile an application that uses this function, define _WIN32_WINNT as 0x0500 or later.

I included _WIN32_WINNT 0x0500.
That, however, will give you an error because _WIN32_WINNT is already define in windef.h as

#ifndef _WIN32_WINNT
#define _WIN32_WINNT WINVER

Which WINVER is defined as

#ifndef WINVER
#define WINVER 0x0400

Bottom line. Include _WIN32_WINNT 0x0500 before <windows.h> and your program will compile fine. It might give you as an output: Access is denied.

Thank you for all of yours for giving a good explanation to me.

Best Regards,

zawpai

This article has been dead for over six months. Start a new discussion instead.