Hi everyone, I made a program that will check if a process is running.. it is supposed to detect if a process is running and start a timer. If the process has been ended, the timer is paused.. when the process is restarted, the timer continues from where it last was. Until it reaches example 5 minutes.. or until the timer count reaches 1 hr.. etc..

The code I have is as follow:

#pragma comment(lib, "advapi32.lib")
#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <tlhelp32.h>
#include <stdio.h>
#include <stdlib.h> 

using namespace std;


DWORD CountProcesses(CHAR *pProcessName) 
{
    DWORD dwCount = 0;
    HANDLE hSnap = NULL;
    PROCESSENTRY32 proc32;

    if((hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)) == INVALID_HANDLE_VALUE)
        return -1;
    proc32.dwSize=sizeof(PROCESSENTRY32);
    while((Process32Next(hSnap, &proc32)) == TRUE)
        if(stricmp(proc32.szExeFile,pProcessName) == 0)
            ++dwCount;
    CloseHandle(hSnap); 
    return dwCount;
}

int main(void)
{
    DWORD dwReturn;
    char cProcess[80] = "hl.exe";		         //process to be checked..

    dwReturn = CountProcesses(cProcess);
    if(dwReturn != -1){
        printf("There are %d %s processes running\n",dwReturn, cProcess);   //check if process is running..
	}
    else{
        printf("CreateToolhelp32Snapshot failed\n");
	}


	if(dwReturn != -1){
		system("taskkill /F /IM hl.exe");		//Stop program from running..
          //would love to change this  something like system("taskkill /F /IM"<<cProcess<<"");
	}

	system("Pause");
    return 0;
}

And the timer I found on the internet, I edited and and added a repeat function but Im not sure how to implement it.. Or is there a far better way of checking how long it was running?

#define REPEAT do{
#define UNTIL( condition ) }while(!(condition));

#include <iostream.h>
#include <conio.h>
#include <windows.h>
#include <time.h>	// class needs this inclusion


//////////////////////////////////////////
// class declaration:


class timer {
	public:
		timer();
		void           start();
		void           stop();
		void           reset();
		bool           isRunning();
		unsigned long  getTime();
		bool           isOver(unsigned long seconds);
	private:
		bool           resetted;
		bool           running;
		unsigned long  beg;
		unsigned long  end;
};


//////////////////////////////////////////
// class implementation:


timer::timer() {
	resetted = true;
	running = false;
	beg = 0;
	end = 0;
}


void timer::start() {
	if(! running) {
		if(resetted)
			beg = (unsigned long) clock();
		else
			beg -= end - (unsigned long) clock();
		running = true;
		resetted = false;
	}
}


void timer::stop() {
	if(running) {
		end = (unsigned long) clock();
		running = false;
	}
}


void timer::reset() {
	bool wereRunning = running;
	if(wereRunning)
		stop();
	resetted = true;
	beg = 0;
	end = 0;
	if(wereRunning)
		start();
}


bool timer::isRunning() {
	return running;
}


unsigned long timer::getTime() {
	if(running)
		return ((unsigned long) clock() - beg) / CLOCKS_PER_SEC;
	else
		return (end - beg) / CLOCKS_PER_SEC;
}


bool timer::isOver(unsigned long seconds) {
	return seconds >= getTime();
}


//////////////////////////////////////////
// class test program:


int main() {
	bool quit = false;
	char choice;
	timer t;
	while(! quit) {
            
               {
					t.start();
					cout << "started" << endl;
					int i = 0;
					do
					{
					      Sleep(1000);
					      cout <<t.getTime()/3600<<" hrs & "<< t.getTime() << " s" << endl;
					      i++;
                    }while(i <= 20);
                    quit = true;
				}
		}
		cout << "------------------------------" << endl;
	return 0;
}

Edited 5 Years Ago by triumphost: n/a

What's the ultimate goal here? Are you just trying to report the uptime of a process, or is this more of a control to keep the process from running continuously for a certain amount of time?

The ultimate goal is to control the overall uptime of a process.. Just an example:

User plays a game for 5 hrs straight.. decides to take a break and plays for 7 hrs straight after the break..

The program is to check total playtime (does not include the break).
the break is where the timer should pause.. If the uptime exceeds a certain amount (example: 20hrs) then stop the process and make sure it does not run until computer restarts.

Thats just an example though but I guess its pretty relevant to what I want..

Basically I was going to make the user enter a password.. the process must never start unless the password is right.. then after a certain amount of time (pause at breaks).. stop process until password entered again. If process started before password entered then exit process.. Ill implement all that later..

Just need to time the total uptime of a process

Edited 5 Years Ago by triumphost: n/a

Let's assume for a moment that you don't need a granularity that would eat up a lot of CPU time (say a 1 minute poll/sleep schedule). You really don't need a timer, just a running count of uptime units as long as the process is running:

int minutes_up()
{
    static int minutes = 0; // Resets on program restart

    if (process_running(process_name))
        ++minutes;

    return minutes;
}

If the process isn't running and you only care about totals, there's nothing to count.

^^OMG GOOD IDEA!! kk I got it to constantly check every few seconds if the process is running..

The only problem now is my password problem..

Any Idea how I can do the following:

cout<<"enter password: ";
cin>> x;

if after 10 seconds the user hasnt entered a password, terminate?

Is that possible??

hmm cant figure it out.. Right now this is what I got..
Problems: Program waits an infinite amount of time for the user to enter a password.. and if process not running, timer doesnt pause.. instead it resets..

might look like a lot to read through but most of it is basically declarations.. program starts at line 113, ends at line 167

#define REPEAT do{
#define UNTIL( condition ) }while(!(condition));

#pragma comment(lib, "advapi32.lib")
#include <windows.h>
#include <tlhelp32.h>
#include <iostream>
#include <conio.h>
#include <string.h>
#include <time.h>	// class needs this inclusion

using namespace std;


//////////////////////////////////////////
// class declaration:


class timer {
	public:
		timer();
		void           start();
		void           stop();
		void           reset();
		bool           isRunning();
		unsigned long  getTime();
		bool           isOver(unsigned long seconds);
	private:
		bool           resetted;
		bool           running;
		unsigned long  beg;
		unsigned long  end;
};


//////////////////////////////////////////
// class implementation:


timer::timer() {
	resetted = true;
	running = false;
	beg = 0;
	end = 0;
}


void timer::start() {
	if(! running) {
		if(resetted)
			beg = (unsigned long) clock();
		else
			beg -= end - (unsigned long) clock();
		running = true;
		resetted = false;
	}
}


void timer::stop() {
	if(running) {
		end = (unsigned long) clock();
		running = false;
	}
}


void timer::reset() {
	bool wereRunning = running;
	if(wereRunning)
		stop();
	resetted = true;
	beg = 0;
	end = 0;
	if(wereRunning)
		start();
}


bool timer::isRunning() {
	return running;
}


unsigned long timer::getTime() {
	if(running)
		return ((unsigned long) clock() - beg) / CLOCKS_PER_SEC;
	else
		return (end - beg) / CLOCKS_PER_SEC;
}


bool timer::isOver(unsigned long seconds) {
	return seconds >= getTime();
}

DWORD CountProcesses(CHAR *pProcessName) 
{
    DWORD dwCount = 0;
    HANDLE hSnap = NULL;
    PROCESSENTRY32 proc32;

    if((hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)) == INVALID_HANDLE_VALUE)
        return -1;
    proc32.dwSize=sizeof(PROCESSENTRY32);
    while((Process32Next(hSnap, &proc32)) == TRUE)
        if(stricmp(proc32.szExeFile,pProcessName) == 0)
            ++dwCount;
    CloseHandle(hSnap); 
    return dwCount;
}

//////////////////////////////////////////
// class test program:
         
void pwget();
void stopped();
void started();
string pword;
string playagain;
int x;
int i = 0;

//int main( int, char *[] )
int main(void)
{
    system("cls");
    cout<<"Welcome to GameBlock v1.0\n\n";
    REPEAT
    pwget();
    
   	bool quit = false;
	char choice;
	timer t;
	
	while(! quit) {
            
               {
                    cout<<"Timer Started\n";  
                    started();
                    quit = true;
                    system("taskkill /IM notepad.exe");
                    cout<<"\n";
				}
		}
		cout << "------------------------------\n\n" << endl;
		cout<<"Play Again??  (y/n): \n\n";
		playagain.clear();
		cin>> playagain;
		cin.ignore();
		if((playagain == "y") || playagain == "Y")
		{
                      main();
        }
        else if ((playagain == "n") || playagain == "N")
        {
                       main();
        }
        else
        {
                       main();
        }
	UNTIL(playagain == "n");
    playagain.clear();
    system("cls");
    
    return 0;
}

void pwget()
{    
     REPEAT
     char cProcess[80] = "notepad.exe";
     DWORD dwReturn;
     dwReturn = CountProcesses(cProcess);
     if(dwReturn != -1)
     {
                           if(dwReturn == 1)
                           {
                              cout<<"User Attempted to Open Program!\n\n";
                              cout<<"Enter The Password To Allow the program to run: ";
                              pword.clear();
                              cin>>pword;
                              cin.ignore();
                                       if (pword == "brandon")
                                       {
                                                 cout<<"Password Correct\n\n";
                                                 system("cls");
                                       }
                                       else if(pword != "brandon")
                                       {
                                           system("taskkill /IM notepad.exe");
                                           cout<<"\n";
                                           system("cls");
                                           cout<<"Welcome to GameBlock v1.0\n\n";
                                           pwget();
                                       }
                                       else
                                       {
                                           system("taskkill /IM notepad.exe");
                                           cout<<"\n";
                                           system("cls");
                                           cout<<"Welcome to GameBlock v1.0\n\n";
                                           pwget();
                                       }
                           }
                           else if(dwReturn == 0)
                           {
                               cout<<"Program Not Running\n";
                               Sleep(1000);
                               main();
                           }
     }
    UNTIL((pword == "brandon"));
    pword.clear();
    cout<<"In Minutes, enter the length of time the program should run: ";
    cin>>x;
    cin.ignore();
    x = x*60;
}

void stopped()
{
	bool quit = false;
	char choice;
	timer t;
	char cProcess[80] = "notepad.exe";
	DWORD dwReturn;
	dwReturn = CountProcesses(cProcess);
    if(dwReturn != -1)
     {
                           if(dwReturn == 0)
                           {
                              cout<<"Program Not running, User taking a break.. Timer Paused...\n\n";
                              Sleep(2000);
                              started();
                           }
                           else if(dwReturn == 1)
                           {
                                //cout<<"Program Started/Resumed.. Timer Resumed..\n\n";
                           }
     }
}

void started()
{
     bool quit = false;
	char choice;
	timer t;
	char cProcess[80] = "notepad.exe";
	DWORD dwReturn;
	dwReturn = CountProcesses(cProcess);
					do
					{
                           Sleep(1000);
                           cout <<t.getTime()/3600<<" hrs & "<< t.getTime() << " s" << endl;

                           DWORD dwReturn;
                           char cProcess[80] = "notepad.exe";
                           dwReturn = CountProcesses(cProcess);
                           if(dwReturn != -1)
                           {
                             stopped();
                             if(dwReturn == 1)
                             {
                              t.start();
                             }
                           }
                           else if(dwReturn == 0)
                           {
                                Sleep(2000);
                               cout<<"Program Not Running... \n\n";
                           }
                      i++;
                    }while(i <= x);
}

Edited 5 Years Ago by triumphost: n/a

For the 10 seconds to enter a password thing you will most likely want to start a new thread that waits for 10 seconds to grab some input.

This question has already been answered. Start a new discussion instead.