Advertisement
wheeler

wxWidgets and boost::asio (std::thread)

Aug 24th, 2014
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.11 KB | None | 0 0
  1. #include "wx/wx.h"
  2. #include <boost/asio.hpp>
  3. #include <iostream>
  4. #include <string>
  5. #include <thread>
  6.  
  7. class app_frame :
  8.     public wxFrame
  9. {
  10.     private:
  11.         wxPanel *panel;
  12.     public:
  13.         app_frame() : wxFrame(0, wxID_ANY, wxT("wxWidgets 3 and boost::asio"), wxPoint(0,0), wxSize(300,600))
  14.         {
  15.         }
  16. };
  17.  
  18. class app :
  19.     public wxApp
  20. {
  21.     private:
  22.         std::thread worker;
  23.     public:
  24.         virtual bool OnInit()
  25.         {
  26.             worker = std::thread([]() {
  27.                 boost::asio::io_service io;
  28.                 boost::asio::ip::tcp::socket sock(io);
  29.                 boost::asio::ip::tcp::resolver res(io);
  30.                 auto end_it = res.resolve({ std::string("localhost"), std::string("5000") });
  31.                 std::cout << "Connecting...\n";
  32.                 boost::asio::async_connect(sock, end_it, [&](boost::system::error_code ec, boost::asio::ip::tcp::resolver::iterator iter) {
  33.                     if (!ec)
  34.                         std::cout << "Connected!!!!\n";
  35.                     else
  36.                         std::cout << "Not connected!!!!\n";
  37.                 });
  38.             });
  39.             frame = new app_frame;
  40.             frame->Show();
  41.             return true;
  42.         }
  43.  
  44.         virtual int OnExit()
  45.         {
  46.             worker.join();
  47.             return 0;
  48.         }
  49.     private:
  50.         app_frame *frame;
  51. };
  52.  
  53. IMPLEMENT_APP(app);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement