Dispose of AddIns created using MAF (System.AddIn)
Asked Answered
P

2

7

Does anyone know how to dispose of AddIns created using System.AddIn. All the examples online seem to show how to easily load and use an addin, but none show how to dispose of them once they're alive. My Problem is I create addins in new processes, and these processes never get garbage collected, obviously a problem.

Below is some sample code illustrating my problem. Assume that the user never exits this application, but instead creates many instances of ICalculator. How do these addIn processes ever get disposed of?

    static void Main(string[] args)
    {
        string addInRoot = GetExecutingDirectory();

        // Update the cache files of the pipeline segments and add-ins
        string[] warnings = AddInStore.Update(addInRoot);

        // search for add-ins of type ICalculator
        Collection<AddInToken> tokens = AddInStore.FindAddIns(typeof(ICalculatorHost), addInRoot);

        string line = Console.ReadLine();
        while (true)
        {
            AddInToken calcToken = ChooseCalculator(tokens);

            AddInProcess addInProcess = new AddInProcess();
            ICalculatorHost calc = calcToken.Activate<ICalculatorHost>(addInProcess, AddInSecurityLevel.Internet);

            // run the add-in
            RunCalculator(calc);    
        }
    }
Playwriting answered 7/10, 2010 at 10:15 Comment(1)
Oh, some extra information. I am also keeping a reference to the contract handle in my HostViewAdapter class, which still doesn't seem to do the trick.Playwriting
P
10

I managed to find a solution to the above problem, it's making use of the AddInController class and it's shutdown method. Now to see if I can get this to work in my application, not just this example:

    static void Main(string[] args)
    {
        string addInRoot = GetExecutingDirectory();
        string[] warnings = AddInStore.Update(addInRoot);
        Collection<AddInToken> tokens = AddInStore.FindAddIns(typeof(ICalculatorHost), addInRoot);


        while (true)
        {
            AddInToken calcToken = ChooseCalculator(tokens);

            AddInProcess addInProcess = new AddInProcess();
            ICalculatorHost calc = calcToken.Activate<ICalculatorHost>(addInProcess, AddInSecurityLevel.Internet);

            // run the add-in
            RunCalculator(calc);

            // shutdown the add-in when the RunCalculator method finishes executing
            AddInController controller = AddInController.GetAddInController(calc);
            controller.Shutdown();
        }
    }
Playwriting answered 8/10, 2010 at 9:19 Comment(3)
+1 for reporting back your own solution. Very much in spirit of SO.Alasteir
Thanks, I thought I'd save my fellow developers the painPlaywriting
"I thought I'd save my fellow developers the pain" - and there is so much pain with System.AddIn....Overtone
C
0

your solution didn't work for me.

Since you are creating a new process for your addin you can simply

addInProcess.Shutdown();

the external process will shutdown

Clay answered 10/3, 2016 at 16:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.