views:

22

answers:

1

Hi all,

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);    
        }
    }
+1  A: 

Hi all,

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();
        }
    }
Anish Patel
+1 for reporting back your own solution. Very much in spirit of SO.
spender
Thanks, I thought I'd save my fellow developers the pain
Anish Patel