Windows C++代码(类似于CRON),每xx小时执行一次命令
Windows C++ Code (similar to CRON) that executes a command every xx hours
在我面临的情况下,我需要C++中的一些代码,这些代码将每2小时执行一个命令,尽管我没有用C++(而不是C#)编程,但在这种情况下我无法使用C#。
有人能提供一个示例代码来演示这一点吗?
也许,像这样简单的事情?:
VOID WINAPI Sleep(
__in DWORD dwMilliseconds
);
while (true)
{
dosmt();
sleep(2*60*60*1000);
}
或者在单个线程中启动它,以防它应该与剩余的程序并行执行?在这种情况下,boost::线程可以提供帮助。
c++标准库不提供任何类似于c#定时器的选项,您可以使用睡眠,但这会挂起线程,
一个不太准确的解决方法是从初始化时的时钟中获取时间
在一些定期执行的块中进行检查,查看time>init+step,然后跳转到您的定时器语句并重置init=cur_time。。
或者你可以使用windows定时器:
http://www.cplusplus.com/forum/beginner/11271/http://www.cplusplus.com/forum/windows/5531/
使用C++服务向导创建服务,并将其插入服务(当然还有更多的错误捕获)。这应该适用于大多数现代版本的Windows。
#include "stdafx.h"
#include <windows.h>
#include <iostream>
using namespace std;
/**
A callback function. It is similar to a delegate in .Net.
*/
VOID CALLBACK theTimerCallback(PVOID aParam, BOOLEAN TimerOrWaitFired)
{
// This is executed when the timer fires.
cout << "The timer says: Hello, world." << endl;
// The parameter (see below) is a handle to single the
// main thread to shut down.
HANDLE theShutdownEvent = (HANDLE)aParam;
// Tell the main thread to shutdown.
SetEvent (theShutdownEvent);
}
int _tmain(int argc, _TCHAR* argv[])
{
// Assuming you have a program running some main thread, this
// will run a timer in the background and handle the timer callbacks.
// So if this is a service, this timer would execute while the main
// service thread can handle startup and shutdown of the service.
// If it is just a single thread of an application that you manually
// execute, then using a sleep in a loop would work fine.
// Creating an event to make this main thread wait.
HANDLE anEventHandle = CreateEvent (NULL, TRUE, FALSE, L"Shutdown event");
// The queue object that handles the timers
HANDLE theTimerQueueHandle = CreateTimerQueue ();
HANDLE theTimerHandle = NULL;
if (CreateTimerQueueTimer (
&theTimerHandle, // The handle to the timer is written to this variable.
theTimerQueueHandle, // The handle to the timer queue that tracks this timer.
theTimerCallback, // The callback function (see above).
anEventHandle, // A parameter sent to the callback function. This can be anything.
10000, // Time to fire, in milliseconds (10 secs).
0, // Execution period - 0 means it only fires once.
WT_EXECUTEDEFAULT // Look at the API docs and pick your own flags.
) )
{
cout << "Main thread waiting for timer." << endl;
// This makes the main thread wait until the timer fires. Normally, something like
// a service would have its own mechanism of waiting on the main thread.
WaitForSingleObject (anEventHandle, INFINITE);
// This shuts down all the timers, deletes their handles, waits for
// handler functions to finish, and deletes the timer handles as well
// as the queue handle.
DeleteTimerQueueEx (theTimerQueueHandle, INVALID_HANDLE_VALUE);
}
CloseHandle (anEventHandle);
cout << "Main thread exiting" << endl;
return 0;
}
相关文章:
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- C++,系统无法执行指定的程序
- 使用C++中的模板和运算符重载执行矩阵运算
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 执行函数时导致崩溃的变量
- 无论条件是否为true,if总是在c++中执行
- 当函数模板参数是具有默认参数的类模板时,函数模板参数的推导如何执行
- 在C++中对T*类型执行std::move的意外行为
- 为什么在读取文件大小时文件IO速度会发生变化
- 使用QProcess执行命令,并将结果存储在QStringList中
- 如何在没有信号的情况下从C++执行QML插槽
- 如何确认我的constexpr表达式实际上已经在编译时执行
- C++17中的并行执行策略
- QML按钮点击功能执行顺序
- 程序在执行程序的其余部分之前退出
- 为什么catch中的代码没有被执行
- C++从其他 constexpr 创建 lambda 不能按顺序执行 Constexpr
- 将执行、作业和WinAPI相乘
- Windows C++代码(类似于CRON),每xx小时执行一次命令
- C++代码每小时执行一次文件