Search code examples
c#maf

Howto host and activate WinForm-AddIns that run out of process (new AddInProcess())?


Fact

I'm trying to make a host application that manages AddIns(legacy WinForm exe's) out of process using MAF (eg. Add-ins and Extensibility)

The AddIn activation is done out of process like:

addinToken.Activate<PluginHostView>(new AddInProcess(), AddInSecurityLevel.FullTrust);

in the host application that keeps track of plugins and shows them in a TooStripMenu. The ToolStripMenuitem.Click event in the host application fires the PlugIn.ShowMainDialog() - method that is part of the AddIn contract:

(sender as PluginToolStripMenuItem).PlugIn.ShowMainDialog();

The AddIn X project (a WinForm application) implements the contract in a class that contains:

public override void ShowMainDialog()
    {
        new Form1().Show();
    }

Running the above host application makes the AddIn X Form and process hang and it is quite difficult to find out what's wrong :-(

Question

Are there any WORKING samples that use new AddInProcess() to host AddIns containing WinForms out there?


Solution

  • Showing Modeless forms out of process in MAF is not very straightforward. The easiest way I have found is to spawn a new thread and do a ShowDialog on that thread:

    Thread t = new Thread(()=>
    {
        var f = new Form1();
        f.ShowDialog();
    });
    t.SetApartmentState(ApartmentState.STA);
    t.Start();