title: Weird behaviour of thread function when inserting an item to a CTreeCtrl
body: <p>I'm working on a IRC download client and downloads can be added by clicking a link, containing server address, port and all other information required to download a file from the irc. I'm saving this information into a file, to reload the download list when the user has quit and restarted the download client. When loading the download list, I'm running a <code>CWinThread</code> function for each download. The function creates a list (vector) of all server connections. If a connection to the appropriate server does not exist it adds a new one accordingly. Everything works as expected, but if I add an item to a <code>CTreeCtrl</code>, the program cycle seems to be way different.</p>
  
  <pre><code>//For each download I'm calling this in a loop
  AfxBeginThread(RequestDownload, (LPVOID)&amp;m_RequestStruct);
  
  //In this function I'm comparing the available server 
  //connections with the required server connection
  //for the download and set a BOOL if it exists
  
  UINT MyProgramDlg::RequestDownload(LPVOID pParam)
  {
      //...
      if (ConnectionExists)
      {
          //Use the connection and 
          //don't add a new one to the list
      }
  
      if (!ConnectionExists)
      {
          //Add a new connection
          ConnectionStruct Connection;
          Connection.Status = STAT_DISCONNECTED;
          Connection.Connecting = FALSE;
          Connection.Connected = FALSE;
          Connection.LoggingOn = FALSE;
          Connection.LoggedOn = FALSE;
          Connection.Listening = FALSE;
          Connection.NetworkName = pRS-&gt;pm_Downloads-&gt;at(DownloadIndex).NetworkName;
          Connection.NetworkAddress = pRS-&gt;pm_Downloads-&gt;at(DownloadIndex).NetworkAddress;
  
          // Insert the tree item and save its handle
          // The following line is the EVIL LINE that makes my program misbehave
          Connection.hTreeItem = pRS-&gt;pm_TreeCtrl-&gt;InsertItem(_T("Test"), 0, 0, NULL, NULL);
  
          // Add the new connection
          pRS-&gt;pm_Connections-&gt;push_back(Connection);
          // (BREAK POINT 1)
  
          // Get the index of the added connection
          ConnectionIndex = (int) pRS-&gt;pm_Connections-&gt;size() - 1;
          // (BREAK POINT 2)
          //...
      }
      //...
  }
  </code></pre>
  
  <p>If I load two downloads from the same server address without(!) the "Evil Line", the debugger jumps from break point 1 to break point 2 and then again from break point 1 to break point 2. But with the evil line being enabled it jumps from break point 1 to break point 1 and then from break point 2 to break point 2.</p>
  
  <p>I desperately need to fix this.
  Any help is appreciated!</p>