我希望该程序在每次启动(调用)10分钟时运行。
但是我没有找到一种解决方案,该解决方案每10分钟调用一次c ++代码(man.exe)。 我想在Visual Studio 2013中使用代码
int runevery() {
system("start man.exe");
return true;
}
致电:
#ifdef MAN_RUN
runevery();
#endif
谢谢您的帮助!
答案 0 :(得分:1)
您可以创建另一个线程,该线程定期执行该功能直到停止。示例:
#include <mutex>
#include <chrono>
#include <thread>
#include <iostream>
#include <functional>
#include <condition_variable>
class PeriodicAction {
std::mutex m_;
std::condition_variable c_;
bool stop_ = false;
std::function<void()> const f_;
std::chrono::seconds const initial_delay_;
std::chrono::seconds const delay_;
std::thread thread_;
bool wait(std::chrono::seconds delay) {
std::unique_lock<std::mutex> lock(m_);
c_.wait_for(lock, delay, [this]() { return stop_; });
return !stop_;
}
void thread_fn() {
for(auto delay = initial_delay_; this->wait(delay); delay = delay_)
f_();
}
public:
PeriodicAction(std::chrono::seconds initial_delay,
std::chrono::seconds delay,
std::function<void()> f)
: f_(move(f))
, initial_delay_(initial_delay)
, delay_(delay)
, thread_(&PeriodicAction::thread_fn, this)
{}
~PeriodicAction() {
this->stop();
thread_.join();
}
void stop() {
{
std::unique_lock<std::mutex> lock(m_);
stop_ = true;
}
c_.notify_one();
}
};
char const* now_c_str() {
auto time_t = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
return std::ctime(&time_t);
}
int main(int ac, char**) {
using namespace std::literals::chrono_literals;
// Print current time for the next 5 seconds and then terminate.
PeriodicAction a(0s, 1s, []() { std::cout << now_c_str(); });
std::this_thread::sleep_for(5s);
}
适用于您的情况:
PeriodicAction a(0s, 600s, [](){ system("start man.exe"); });
答案 1 :(得分:0)
我认为这不是一个好主意,但是很容易实现:
#include <thread>
#include <chrono>
int main()
{
while (true)
{
std::this_thread::sleep_for(std::chrono::minutes(10));
system("man.exe");
}
}
我仍然认为,按照我先前的评论,Windows上的预定任务将表现得更好,并且更易于配置。