Thread: calculating program uptime

  1. #1
    Registered User Rare177's Avatar
    Join Date
    May 2004
    Posts
    214

    calculating program uptime

    would some one have an example of how to calculate how long a program has been running?

    say one i make and on startup it starts a timer or something
    Last edited by Rare177; 07-15-2004 at 11:08 AM.

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    One possibility using timeGetTime:
    Code:
    /*use of timeGetTime - link with winmm.lib (MinGW -lwinmm)*/
    
    #include <windows.h>
    #include <tchar.h>
    
    int WINAPI _tWinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
                         LPTSTR lpCmdLine,int nCmdShow)
    {
    DWORD               start,finish,elapsed;
    STARTUPINFO         si;
    PROCESS_INFORMATION pi;
    TCHAR               time_taken[128],title[]=_T("Use of timeGetTime");
    
    ZeroMemory(&si,sizeof(si));
    si.cb=sizeof(si);
    ZeroMemory(&pi,sizeof(pi));
    
    start=timeGetTime(); /*start timer*/
    /*Start other program, for example, the windows calculator.*/
    if(!CreateProcess(0,_T("calc.exe"),0,0,0,0,0,0,&si,&pi)) 
      {
      MessageBox(0,"error",title,MB_OK);
      return 0;
      }
    
    /*Wait for child process to quit */
    WaitForSingleObject(pi.hProcess,INFINITE);
    
    /*and clean up its garbage*/
    CloseHandle(pi.hProcess);
    CloseHandle(pi.hThread);
    
    /*stop timer, calculate and display elapsed time*/
    finish=timeGetTime();
    elapsed=finish-start;
    wsprintf(time_taken,_T("Elapsed time: %u milliseconds"),elapsed);
    
    MessageBox(0,time_taken,title,MB_OK);
    return 0;
    }
    Search the boards as there should be plenty of other examples.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  2. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  3. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  4. Taking input while calculating
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 07-12-2002, 04:47 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM