每隔xx小时执行一次命令的Windows C ++代码类似于CRON

问题描述

|| 在我遇到的情况下,我需要一些C ++代码,每2小时执行一次命令,尽管我不是用C ++(而是C#)编写程序,但是在这种情况下,我无法使用C#。 有人可以提供示例代码来说明这一点吗?     

解决方法

        可能是这样简单吗?:
VOID WINAPI Sleep(
__in  DWORD dwMilliseconds
);
while (true)
{
   dosmt();
   sleep(2*60*60*1000);
}
还是在单个线程中启动它以防应与其余程序并行执行?在这种情况下,boost :: thread可以提供帮助。     ,        c ++标准库没有提供任何类似于c#计时器的选项,您可以使用sleep,但是那样会挂起线程, 不太准确的解决方法是从初始化时的时钟获取时间, 并将检查放在某个定期执行的块中,以查看时间是否大于“ 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;
}