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);
}
}
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();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With