title: C++ updating windows window from async thread
body: <p>So I just started with C++ and wanted to create a window with a button that starts an asynchronous thread for a counter that counts from 5 to 0, representing a long time consuming task. The number should've been shown on the Window and get updated every second while the counter is counting. For that the child thread has to communicate in any way with the Message Loop of the main window thread. 
  I tried to do this by: </p>
  
  <ul>
  <li>Sending an UpdateWindow with the windowhandle of the main window</li>
  <li>Sending an PostMessage with the windowhandle of the main window</li>
  </ul>
  
  <p>But in both cases, the window does not get updatet. So I'm suspecting an error by either sending the window handle from the main thread to the child thread or sending the UpdateWindow message from the child thread to the main thread or both or I'm completely off track and everythig is wrong. </p>
  
  <p>Maybe my way of thinking is also wrong and i should do that on another way, still, i don t know how i even should start.</p>
  
  <pre><code>#include "stdafx.h"
  #include "Testproject.h"
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;thread&gt;
  
  #define MAX_LOADSTRING 100
  
  // Global variables:
  HINSTANCE hInst;                                // Aktuelle Instanz
  WCHAR szTitle[MAX_LOADSTRING];                  // Titelleistentext
  WCHAR szWindowClass[MAX_LOADSTRING];            
  HWND Button1;
  int i = 0;
  </code></pre>
  
  <p>My Counter:</p>
  
  <pre><code>void counterr(HWND hWnd)
  {
      i = 5;
      while(i&gt;0)
      {
  
      i -= 1;
      //UpdateWindow(hWnd);
      PostMessage(hWnd, WM_PRINT, NULL, NULL);
      Sleep(1000);
  
      }
  }
  </code></pre>
  
  <p><em>standard window and message loop things from VisualStudio2017</em></p>
  
  <pre><code>LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  {
      switch (message)
      {
      case WM_CREATE:
      {
          Button1 =   CreateWindow(L"Button",L"Counter",WS_VISIBLE|WS_CHILD|WS_BORDER,0,40,100,20,hWnd,(HMENU) 1,nullptr,nullptr);
  
      break;
  }
  case WM_COMMAND:
      {
          int wmId = LOWORD(wParam);
          // Menüauswahl bearbeiten:
          switch (wmId)
          {
          case IDM_ABOUT:
              DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
              break;
          case IDM_EXIT:
              DestroyWindow(hWnd);
              break;
          case 1:
          {
              std::thread t1(counterr, hWnd);
              t1.detach();
              break;
          }
          default:
              return DefWindowProc(hWnd, message, wParam, lParam);
          }
      }
      break;
  case WM_PRINT:
  case WM_PAINT:
      {
          PAINTSTRUCT ps;
          HDC hdc = BeginPaint(hWnd, &amp;ps);
          //TODO: Zeichencode, der hdc verwendet, hier einfügen...
          RECT rc;
          RECT rc2 = { 0, 0, 0, 0 };
          int spacer = 3;
          GetClientRect(hWnd, &amp;rc);
  
          SelectObject(hdc, GetStockObject(DEFAULT_GUI_FONT));
          SetBkMode(hdc, TRANSPARENT);
          SetTextColor(hdc, RGB(0, 0, 0));
  
  
  
          std::wstring strOut = std::to_wstring(i); // or wstring if you have unicode set
          DrawText(hdc, strOut.c_str(), strOut.length(), &amp;rc, DT_SINGLELINE);
          DrawText(hdc, strOut.c_str(), strOut.length(), &amp;rc2, DT_CALCRECT);
          rc.left = rc.left + rc2.right + spacer;
          std::wstring strOut2 = L"heya";
          DrawText(hdc, strOut2.c_str(), strOut2.length(), &amp;rc, DT_TOP | DT_LEFT | DT_SINGLELINE);
  
  
          EndPaint(hWnd, &amp;ps);
      }
      break;
  case WM_DESTROY:
      PostQuitMessage(0);
      break;
  default:
      return DefWindowProc(hWnd, message, wParam, lParam);
  }
  return 0;
  }
  </code></pre>
  
  <p><em>standard stuff again and end of Code</em></p>