在我面臨的情況下,我需要一些C++代碼,每隔2小時執行一次命令,儘管我沒有編程C++(而不是C#),但在這種情況下,我無法使用C#。Windows C++代碼(與CRON類似)每xx小時執行一次命令
有人能提供演示此請
在我面臨的情況下,我需要一些C++代碼,每隔2小時執行一次命令,儘管我沒有編程C++(而不是C#),但在這種情況下,我無法使用C#。Windows C++代碼(與CRON類似)每xx小時執行一次命令
有人能提供演示此請
C++標準庫不提供類似於C#計時器的任何選項,您可以使用睡眠,但會掛起線程,
一個不可─一個示例代碼所以非常準確的解決方法是從初始化時鐘獲得時間,並且在一些常規執行的塊中放置一個檢查,看看時間> init +步驟,然後跳轉到你的定時器語句並重置init = cur_time ..
,或者你可以使用windows定時器:
http://www.cplusplus.com/forum/beginner/11271/ http://www.cplusplus.com/forum/windows/5531/
也許,簡單的東西像這樣?:
VOID WINAPI Sleep(
__in DWORD dwMilliseconds
);
。
while (true)
{
dosmt();
sleep(2*60*60*1000);
}
或者在單線程中啓動它,以防其應該與剩餘的程序並行執行?這種情況boost :: thread可以提供幫助。
使用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;
}
對於哪個平臺? – Puppy 2011-06-11 21:55:18
@DeadMG Windows,但在這種情況下任務計劃程序不是一個選項 – James 2011-06-11 22:00:53
使用Windows計時器 – 2011-06-11 22:07:31