InfoPath FormControl on STA worker thread RRS feed

  • Question

  • I have a .NET class that exposes two public methods: one to create an InfoPath form and another to export to one of the supported formats. I’m using the Microsoft FormControl to do this. It's hosted by a Form that does not get displayed. I get called by a Winforms app, but on a MTA worker thread instead of the UI thread. So I create a STA thread and execute on that, which works exactly once and then results in this exception:

    System.InvalidOperationException was unhandled
    Message="Unable to get the window handle for the 'FormControl' control.
    Windowless ActiveX controls are not supported."
    at System.Windows.Forms.AxHost.EnsureWindowPresent()
    at System.Windows.Forms.AxHost.InPlaceActivate()
    at System.Windows.Forms.AxHost.TransitionUpTo(Int32 state)
    at System.Windows.Forms.AxHost.CreateHandle()
    at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
    at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
    at System.Windows.Forms.AxHost.EndInit()
    at InfoPathCreateStaThreadTest.FormControlHost.InitializeComponent()
    C:\Windows\Temp\InfoPathCreateStaThreadTest\InfoPathCreateStaThreadTest\FormControlHost.Designer.cs:line 65

    After some experimenting, I started to suspect this is a message pumping problem and then came across this: . So I tried various methods of pumping messages, but with no luck. I should mention that the alternative of automating the InfoPath app is not viable unless I can figure out how to hide the app.

    Any help would be much appreciated.
    Saturday, February 21, 2009 2:43 AM