title: Thoughts on a different way to running a win32 event loop without WndProc?
body: <p>Whilst messing around with multithreading, callbacks, win32 api functions, and other troublesome troubles, I received an idea event. (hehehe)</p>
  
  <p>What if, instead of defining a global (or static when designing a class) callback function, I instead assigned <code>DefWindowProc</code> for <code>lpfnWndProc</code> when registering the window class, and then ran the whole event loop on a separate thread?</p>
  
  <p>This way I don't have to hack around the <code>this</code> problem when <a href="http://www.infernodevelopment.com/c-win32-api-simple-gui-wrapper" rel="nofollow">implementing the callback in a class</a>
  and the main thread's execution continues, freeing you from that god-forsaken while loop, allowing you to do whatever, even open another window (yay!)</p>
  
  <p><b>The "normal" way:</b></p>
  
  <pre><code>LRESULT CALLBACK WndProc(...)
  {
      ... // process event information
      return DefWindowProc(...);
  }
  
  int CALLBACK WinMain(...)
  {
      ... // initialize whatever needs initializing :)
      WNDCLASSEX wc;
      ...
      wc.lpfnWndProc = WndProc;
      ... // register the class, create the window, etc...
  
     MSG msg;
     while(GetMessage(&amp;msg, 0, 0, 0) != 0)
     {
          ... // TranslateMessage(&amp;msg) if you want/need it
          DispatchMessage(&amp;msg); // dispatches the message to WndProc
     }
  
     return static_cast&lt;int&gt;(msg.wParam);
  }
  </code></pre>
  
  <p><b>My newfound awesome way:</b></p>
  
  <pre><code>DWORD WINAPI MyAwesomeEventLoop(void* data) // must be static in a class
  {
      ... // do whatever you need with the data
      MSG msg;
      while(GetMessage(&amp;msg, 0, 0, 0) != 0)
      {
          ... // TranslateMessage(&amp;msg) if you want/need it
          ... // process event information
              // call PostQuitMessage(0) to leave the loop
      }
  
      return static_cast&lt;DWORD&gt;(msg.wParam);
   }
  
  int CALLBACK WndProc(...)
  {
      ...
      WNDCLASSEX wc;
      ...
      wc.lpfnWndProc = DefWindowProc;
      ...
      HANDLE threadHandle = 0;
      // use "this" as the 4th parameter when implementing in a class
      threadHandle = CreateThread(0, 0, MyAwesomeEventLoop, 0, 0, 0);
  
      ... // you are now free to do whatever you want! :)
  
      // waits untill the thread finishes
      // hopefully because PostQuitMessage(0) was called
      WaitForSingleObject(threadHandle, INFINITE);
      DWORD returnValue = 0;
      GetExitCodeThread(threadHandle, &amp;returnValue);
      CloseHandle(threadHandle);
      ...
  
      return static_cast&lt;int&gt;(returnValue);
   }
  </code></pre>
  
  <p><b>What do you guys think?</b></p>