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
This is a discussion on calculating program uptime within the Windows Programming forums, part of the Platform Specific Boards category; would some one have an example of how to calculate how long a program has been running? say one i ...
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.
One possibility using timeGetTime:Search the boards as there should be plenty of other examples.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; }
CProgramming FAQ
Caution: this person may be a carrier of the misinformation virus.