Dispose of AddIns created using MAF (System.AddIn)
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开发者_StackOverflow社区 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();
}
}
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
精彩评论