title: ATL COM: Function to Return when Callback has been Executed
body: <h2>Background:</h2>
  <p>I'm implementing a COM interface to an existing legacy C++ MFC application. The application is multi-threaded using one thread (main thread) for COM / GUI and one for incoming events from a C-library. The COM interface reflects the possible interactions the user can do using the GUI.</p>
  <p>Edit: The COM server is &quot;out-of-process&quot; i.e. COM is implemented by the MFC application.</p>
  <h2>Problem description:</h2>
  <p>The COM interface has a function to select an object in a tree-view controller. When an object is selected the user can call COM functions that will use the object to execute different functionality.</p>
  <h3>Example 1 - What the user see / feel:</h3>
  <ol>
  <li>Object X is selected in the tree-view using function Y from the COM interface.</li>
  <li>X is highlighted in the GUI and function Y returns.</li>
  <li>User calls Function Z that will delete object X.</li>
  </ol>
  <p>The MFC application is written such that the model many times is tightly coupled with the GUI callback functions for various reasons. For the SelectObject function this means that the object isn't truly selected in the tree-view until a callback function has been fully executed. This means that Example 1 in reality look more like:</p>
  <h3>Example 2 - What really happens:</h3>
  <ol>
  <li>Object X is selected in the tree-view using function Y from the COM interface.</li>
  <li>Function Y set object X to be selected and Y returns.</li>
  <li>A callback function (could be OnSelChanged()) for the tree-view item is called and update all the GUI elements and application model to select X.</li>
  <li>User calls Function Z that will delete object X.</li>
  </ol>
  <p>This isn't a problem for the user if when normal GUI interaction is used; the callback function is called in a very short amount of time after the user select object X in the tree-view so function Z will remove object X.</p>
  <p>When using the COM interface this becomes a problem. When function Y returns the user can chose to immediately call function Z. If the callback function has been called object X will be deleted, otherwise an unexpected behavior will occur.</p>
  <p>This is unwanted behavior. A function like SelectObject indicates to the user that when the function has finished executing, an object should be selected without any extra wait.</p>
  <h2>Solution ideas</h2>
  <p>I have been trying to figure out solutions to this problem and came up with some possible ideas:</p>
  <ol>
  <li>Send event when the callback has finished
  <ul>
  <li>This is unwanted because I still think that when function SelectObject is done executing the user should expect object X to be selected, the user shouldn't have to wait.</li>
  </ul>
  </li>
  <li>Rewrite and separate the GUI and model.
  <ul>
  <li>Very much something I would like, but there's not time for that. The way the GUI is updated is very much coupled with everything else in the application and would require huge changes.</li>
  </ul>
  </li>
  <li>Have COM and GUI running on a separate thread.
  <ul>
  <li>This one is interesting. I don't know if it's possible; I haven't found any information that such a separation is possible. It seems COM is locked to work on the main thread only.</li>
  </ul>
  </li>
  </ol>
  <p>Number 3 is interesting but I lack the knowledge. Has anyone had any similar problem that they solved? I would really like some help with this problem :)</p>