开发者

Using Boost's ASIO, how can I wait on a Windows Event?

My program needs to gracefully terminate when a Windows event becomes signaled. I am using Boost's ASIO library for it's sockets. I 开发者_如何学编程only have one io_service object. How can I 'register' this event handle with the io_service, so it calls a callback when the event signals?


If you're looking for termination handling on Windows for Boost.Asio you can take a look at the examples here.

In short, you need to handle the win events and call stop on your system.

BOOL WINAPI console_ctrl_handler(DWORD ctrl_type)
{
  switch (ctrl_type)
  {
    case CTRL_C_EVENT:
    case CTRL_BREAK_EVENT:
    case CTRL_CLOSE_EVENT:
    case CTRL_SHUTDOWN_EVENT:
      console_ctrl_function();
      return TRUE;
    default:
      return FALSE;
  }
}

This uses a function object:

boost::function0<void> console_ctrl_function;

that you need to bind to your system's shutdown/stop routine.

// Set console control handler to allow server to be stopped.
console_ctrl_function = boost::bind(&your_system::shutdown, &s);
SetConsoleCtrlHandler(console_ctrl_handler, TRUE);


Beginning with Boost 1.49 it appears that it is possible to invoke a handle when an event becomes signaled, see http://www.boost.org/doc/libs/1_49_0/doc/html/boost_asio/overview/windows/object_handle.html.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜