Visualizzazione dei risultati da 1 a 5 su 5
  1. #1

    C++ Programma per calcolare il tempo

    Salve,
    sto cercando di fare un Programma per calcolare quanto tempo resta aperto un altro programma.

    Per esempio: io sto creando il programma A che appena il programma B si apre, il programma A fa partire il tempo, e quando il programma B si chiude, il tempo si ferma.

    Ho provato vari metodi...con i threads...ma non ci sono ancora riuscito....

    Ecco qui il codice
    C++
    codice:
    #include <irrlicht.h>
    #include <iostream>
    #include <windows.h>
    #include <time.h>
    #include <tlhelp32.h>
    #include <ctime>
    #include <stdio.h>
    #include <stdlib.h>
    
    using namespace irr;
    using namespace std;
    
    #pragma comment(lib, "Irrlicht.lib")
    #pragma once
    
    
    int main()
    {
    
    	DWORD FirstGTC = GetTickCount();
    
    	bool bFlag;
    
    	bFlag = false;
    
    	HWND hWnd;
    	DWORD dwID;
    	HANDLE ProcHandle;
    
    	hWnd = FindWindow(NULL, "Euro Truck Simulator");
    
    	GetWindowThreadProcessId(hWnd, &dwID);
    
    	ProcHandle = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE, NULL, dwID);
    
    	if (ProcHandle) {
              FirstGTC = GetTickCount();
              MyThread = CreateThread(&ThreadProc);
         }
    
         while (!bFlag) {
              Sleep(100);
         }
    
         printf("The process was running for %u milliseconds.\n",TimeElapsedInMilliseconds);
    
    	return 0;
    
    }
    
    ThreadProc {
         while (ProcHandle) {
              Sleep(100);
         }
    
         TimeElapsedInMilliseconds = (GetTickCount() - FirstGTC);
         bFlag = true;
    }

  2. #2
    codice:
    #include <iostream>
    #include <windows.h>
    
    using namespace std;
    
    int main()
    {
        DWORD FirstGTC;
        HWND hWnd;
        DWORD dwID=0;
        HANDLE ProcHandle;
    
        hWnd = FindWindow(NULL, "Euro Truck Simulator");
        if(!hWnd)
        {
            cerr<<"Cannot find the window."<<endl;
            return 1;
        }
        GetWindowThreadProcessId(hWnd, &dwID);
        ProcHandle = OpenProcess(SYNCHRONIZE, NULL, dwID);
        if(!ProcHandle)
        {
            cerr<<"Cannot open the process."<<endl;
            return 2;
        }
        FirstGTC = GetTickCount();
        WaitForSingleObject(ProcHandle,INFINITE);
        cout<<"The process was running for "<<(GetTickCount()-FirstGTC)<<" milliseconds."<<endl;
        return 0;
    }
    Amaro C++, il gusto pieno dell'undefined behavior.

  3. #3
    Occhio che la GetTickCount:
    http://msdn.microsoft.com/en-us/libr...08(VS.85).aspx
    The elapsed time is stored as a DWORD value. Therefore, the time will wrap around to zero if the system is run continuously for 49.7 days. To avoid this problem, use GetTickCount64. Otherwise, check for an overflow condition when comparing times.

  4. #4
    grazie raga...ho risolto con una dll.

  5. #5
    Giusto... ma si risolve facilmente.
    codice:
    #include <iostream>
    #include <windows.h>
    
    using namespace std;
    
    int main()
    {
        DWORD lastGTC;
        ULONGLONG totTime=0;
        HWND hWnd;
        DWORD dwID=0;
        HANDLE ProcHandle;
    
        hWnd = FindWindow(NULL, "Euro Truck Simulator");
        if(!hWnd)
        {
            cerr<<"Cannot find the window."<<endl;
            return 1;
        }
        GetWindowThreadProcessId(hWnd, &dwID);
        ProcHandle = OpenProcess(SYNCHRONIZE, NULL, dwID);
        if(!ProcHandle)
        {
            cerr<<"Cannot open the process."<<endl;
            return 2;
        }
        lastGTC = GetTickCount();
        while(WaitForSingleObject(ProcHandle,60000)==WAIT_TIMEOUT)
        {
            totTime+=GetTickCount()-lastGTC;
            lastGTC = GetTickCount();        
        };
        cout<<"The process was running for "<<totTime+(GetTickCount()-lastGTC)<<" milliseconds."<<endl;
        return 0;
    }
    grazie raga...ho risolto con una dll.
    Perché tirare in ballo una dipendenza aggiuntiva quando il problema è di così facile soluzione?
    Amaro C++, il gusto pieno dell'undefined behavior.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.