Tech Off Post

Single Post Permalink

View Thread: WPF + WindowsFormsHost + .Net Forms + ( COM interfaces + Forms Invoke ) = E_NOINTERFACE Exception
  • User profile image

    pompomJuice said:
    pompomJuice said:

    Ok I have fixed the errors. It seems that I needed to pay closer attention when fiddeling with STAThread and MTAThread attributes.

    The errors go away setting the creation/access threads to MTA, but ensuring all threads accessing the COM objects are MTA is tedious, as any events (OnMouseclick for instance) triggered from STA (Forms UI) required me to make worker threads that are MTA to do the work using COM interfaces. These MTA worker threads make calls to Form UI again so again more wrappers for invoke were required.

    So the original problem described at the top was caused by the fact that Control.Invoke spawned a STAThread, which  caused that E_NOINTERFACE error. Therefor the mistake was made by me, or the programmer not making sure that all COM access originated from MTAThread. The original implementation onto Windows CE Masked this problem as windows CE does not seem to have a problem with this.

    The port is compete, no exceptions, but the hackinng required to make it work must have broken something. Normal controls work fine but the map drawing failes for some reason. At least I understand the COM now.

    A correction:

    Control.Invoke does not spawn a new thread. Control.Invoke does some "magic" that causes the "invoked" code to execute on the thread that created the control (tipically the main thread).

    And a note:

    I'm not sure what was your solution after all. Creating a new thread every time you need to call into the COM component? That's scary! Smiley