How i make a Timer for run all 10 min a Command

07/31/2014 15:21 oOAiRMaXOo.#1
Hey Guys i need help how i make a loop timer what run all 10 min a command


like
printf("Test")
after 10 min he send self and this the full time.


thx
07/31/2014 15:47 ​Tension#2
Code:
while(true) {
 printf("Test");
 Sleep(600000);
}
you could put that into a thread.
07/31/2014 17:27 Tasiro#3
Quote:
Originally Posted by ​Tension View Post
Code:
Sleep(600000);
If the compiler supports UDLs and the time literals:
Code:
#include <chrono>
#include <thread>
// ...
using namespace std::literals::chrono_literals;
std::this_thread::sleep_for (10min);
If not:
Code:
#include <chrono>
#include <thread>
#include <initializer_list>
// ...
std::this_thread::sleep_for (std::chrono::minutes {10});
That doesn't require windows.
08/08/2014 11:24 Computerfreek#4
Quote:
Originally Posted by Tasiro View Post
If the compiler supports UDLs and the time literals:
Code:
#include <chrono>
#include <thread>
// ...
using namespace std::literals::chrono_literals;
std::this_thread::sleep_for (10min);
If not:
Code:
#include <chrono>
#include <thread>
#include <initializer_list>
// ...
std::this_thread::sleep_for (std::chrono::minutes {10});
That doesn't require windows.
Nur rein aus interesse: Gibts einen bestimmten Grund, warum du die Minuten als initializer list angibst? An der Stelle würde es eine einfache Klammer doch genau so tun.
08/08/2014 22:29 Tasiro#5
Quote:
Originally Posted by Computerfreek View Post
Nur rein aus interesse: Gibts einen bestimmten Grund, warum du die Minuten als initializer list angibst? An der Stelle würde es eine einfache Klammer doch genau so tun.
Das ist richtig. Aber so wird das nicht mit einem "gewöhnlichen" Funktionsaufruf verwechselt, noch kommt der Compiler auf die Idee, das als Funktionsdeklaration auszugeben (hier natürlich nicht). Wenn nicht der Aufruf eines expliziten Konstruktors das Ziel ist oder Mehrdeutigkeiten entstehen würden, kann der Typ auch weggelassen werden, wenn geschweifte Klammern verwendet werden. Und wenn man das deshalb immer so macht...
08/11/2014 17:29 MaBarker#6
Im a C++ noob too.
So im not sure if it is the best for you but eventually this can help you ;)

Code:
#include <iostream>
#include <Windows.h>
#include <time.h>

void wait(long seconds)
{
	seconds = seconds * 1000;
	Sleep(seconds);
}

int main(void)
{

	std::cout << "..." << std::endl;
	wait(5);
	return 0;
}
And if you want 10 Minutes you can form the seconds into minutes.

Behind the seconds = seconds * 1000 just a * 60.

seconds = seconds * 1000 * 60;
Because then you multiple 60 times the 1 Second (= 1 Minute ^.^)

I hope it was a little help for you :p :D
08/11/2014 18:46 Mostey#7
Quote:
Originally Posted by MaBarker View Post
Ich bin selber noch ein noob in C++ ^^
Also ich kenne mich auch noch net besonders damit aus aber ich würde das ganze so machen:

Code:
#include <iostream>
#include <Windows.h>
#include <time.h>

void wait(long seconds)
{
	seconds = seconds * 1000;
	Sleep(seconds);
}

int main(void)
{

	std::cout << "..." << std::endl;
	wait(5);
	return 0;
}
Und da du ja 10 Minuten haben möchtest würde ich das ganze auch in Minuten takt sozusagen angeben.

Also hinter das seconds = seconds * 1000 einfach ein * 60 ergänzen.

seconds = seconds * 1000 * 60;
Zuerst sei mal gesagt das der TE dich nicht versteht weil er deine Sprache (offensichtlich) nicht spricht.

Dann ist deine Lösung unschön weil sie erstens blockt und zweitens Sleep nicht wirklich genau ist. Achja, und es ist ebenfalls nicht portabel.

Quote:
Note that with Sleep()/nanosleep(), the OS only guarantees that the process suspension will last for at least as long as you specify. The execution of other processes can always delay resumption of your process.
[Only registered and activated users can see links. Click Here To Register...]

Wobei das hier vermutlich keine all zu große Rolle spielt.
08/11/2014 19:39 MaBarker#8
Ups mit der Sprache hab ich gefailt sry.
Werds gleich editen.

Und das mit der Sleep Funk. werd ich mir gleich nochmal gründlich durchlesen aber danke dafür ;)
08/13/2014 01:25 oOAiRMaXOo.#9
My Problem i cant use Sleep this its my problem after i use seelp stop all
08/13/2014 14:20 MaBarker#10
Can you give us the code where you try to use it ?
Eventually you have a little fail ;)
08/15/2014 11:38 wurstbrot123#11
Code:
DWORD starttimer = GetTickCount();

// Insert loop ? 

if( GetTickCount() - starttimer >= 60000 ) // 10 min have passed
{
        // do something
        starttimer = GetTickCount(); // restart timer
}
08/19/2014 12:22 buFFy!#12
Quote:
Originally Posted by wurstbrot123 View Post
Code:
DWORD starttimer = GetTickCount();

// Insert loop ? 

if( GetTickCount() - starttimer >= 60000 ) // 10 min have passed
{
        // do something
        starttimer = GetTickCount(); // restart timer
}
fuck the winapi.

Code:
#ifndef TIMER_H
#define TIMER_H

#include <chrono>

template <typename T>
class Timer
{
private:
	std::chrono::time_point<std::chrono::high_resolution_clock> tStart;
public:

	Timer()
	{
		tStart = std::chrono::high_resolution_clock::now();
	}

	__int64 diff()
	{
		return std::chrono::duration_cast<T>(std::chrono::high_resolution_clock::now() - tStart).count();
	}

	void reset()
	{
		tStart = std::chrono::high_resolution_clock::now();
	}
};

#endif
Code:
Timer<std::chrono::minutes> timer;
if( timer.diff() > 10 ) { .. }