Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/162.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何使用boost';s deadline_timer::asynch_在循环中等待?_C++_Multithreading_Boost_Mfc_Boost Asio - Fatal编程技术网

C++ 如何使用boost';s deadline_timer::asynch_在循环中等待?

C++ 如何使用boost';s deadline_timer::asynch_在循环中等待?,c++,multithreading,boost,mfc,boost-asio,C++,Multithreading,Boost,Mfc,Boost Asio,我有一个MFC应用程序(VC10),其中我在一个单独线程的while循环中使用deadline_timer::asynch_wait()。事实上,等待时间可能是几分钟甚至更长,我使用asych_wait来中断等待。我的问题是,在第一次执行时,计时器回调只被调用一次。当循环第二次执行时,不再调用回调。知道我做错了什么吗 header: std::unique_ptr<boost::asio::deadline_timer> m_timer; cpp: UINT camThread1(

我有一个MFC应用程序(VC10),其中我在一个单独线程的while循环中使用deadline_timer::asynch_wait()。事实上,等待时间可能是几分钟甚至更长,我使用asych_wait来中断等待。我的问题是,在第一次执行时,计时器回调只被调用一次。当循环第二次执行时,不再调用回调。知道我做错了什么吗

header:
std::unique_ptr<boost::asio::deadline_timer> m_timer;

cpp:
UINT camThread1( LPVOID pParam )
{
  CDlg* pView = (CMyDlg*) pParam;
  boost::asio::io_service io_service;

  // to be able to interrupt the timer from the GUI Thread
  pView->m_timer.reset(new boost::asio::deadline_timer(io_service)); 

  boost::posix_time::ptime lastLoop;
  typedef boost::asio::time_traits<boost::posix_time::ptime> time_traits_t;
  lastLoop = time_traits_t::now();

  while (1)
  {
    boost::posix_time::ptime nextFrameTime;
    // calculate the time to wait
    nextFrameTime = lastFrameTime + boost::posix_time::seconds(pView->m_interval);

    // start the timer
    pView->m_timer->expires_at(nextFrameTime);
    pView->m_timer->async_wait(boost::bind(&CDlg::timer_handler, pView, boost::asio::placeholders::error));
    io_service.run();

    DWORD dwWaitResult = WaitForSingleObject(pView->m_timerEvent, INFINITE);
    switch (dwWaitResult) 
    {
      // check results
    }
    lastLoop = time_traits_t::now(); // get the current time

    // do something
  }
}
我的事件创建如下:

void CDlg::timer_handler(const boost::system::error_code& error)
{
  if (!error || error == boost::asio::error::operation_aborted)
  {
    SetEvent(m_timerEvent);
  } else {
    //throw std::exception(...);
  }
}
m_timerEvent = CreateEvent( NULL, FALSE, FALSE, TEXT("timerEvent"));
有什么建议吗?请

Tnx预告&干杯
greg

我想我在仔细阅读了一些教程/示例和文档后解决了这个问题。 首先,io_service.run()调用一直处于阻塞状态,直到所有工作都完成并且没有更多的处理程序需要调度。这使得使用事件的信号不必要

其次,io_服务.run()需要在任何第二次或以后的调用之前重置! 它现在可以使用以下代码:

UINT camThread1( LPVOID pParam )
{
CDlg* pView = (CMyDlg*) pParam;
boost::asio::io_service io_service;

// to be able to interrupt the timer from the GUI Thread
pView->m_timer.reset(new boost::asio::deadline_timer(io_service)); 

boost::posix_time::ptime lastLoop;
typedef boost::asio::time_traits<boost::posix_time::ptime> time_traits_t;
lastLoop = time_traits_t::now();
while (1)
{
  boost::posix_time::ptime nextFrameTime;
  // calculate the time to wait
  nextFrameTime = lastFrameTime + boost::posix_time::seconds(pView->m_interval);

  // start the timer
  pView->m_timer->expires_at(nextFrameTime);
  pView->m_timer->async_wait(boost::bind(&CDlg::timer_handler, pView, boost::asio::placeholders::error));
  if(io_service.stopped()) {
    io_service.reset();
  }
  io_service.run();

  lastLoop = time_traits_t::now(); // get the current time

  // do something
}
UINT凸轮螺纹1(LPVOID pParam)
{
CDlg*pView=(CMyDlg*)pParam;
boost::asio::io_服务io_服务;
//能够从GUI线程中断计时器
pView->m_timer.reset(新的boost::asio::deadline_timer(io_服务));
boost::posix_time::ptime lastLoop;
typedef boost::asio::time\u traits time\u traits\t;
lastLoop=time\u traits\u t::now();
而(1)
{
boost::posix_time::ptime nextFrameTime;
//计算等待的时间
nextFrameTime=lastFrameTime+boost::posix_时间::秒(pView->m_间隔);
//启动计时器
pView->m_timer->expires_at(nextFrameTime);
pView->m_timer->async_wait(boost::bind(&CDlg::timer_handler,pView,boost::asio::placeholders::error));
if(io_service.stopped()){
io_service.reset();
}
io_service.run();
lastLoop=time\u traits\u t::now();//获取当前时间
//做点什么
}

那么,什么是计时器处理程序?这是截止时间计时器的处理程序。计时器过期时将调用该处理程序。请参阅上面的问题,但除此之外,无需设置任何事件!