7

有谁知道如何处理使用 System.AddIn 创建的插件。所有在线示例似乎都展示了如何轻松加载和使用插件,但没有一个展示如何在它们存在后处理它们。我的问题是我在新进程中创建插件,而这些进程永远不会被垃圾收集,这显然是一个问题。

下面是一些说明我的问题的示例代码。假设用户从未退出此应用程序,而是创建了许多 ICalculator 实例。这些 addIn 进程是如何被处理掉的?

    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);    
        }
    }
4

2 回答 2

10

我设法找到了解决上述问题的方法,它使用了 AddInController 类和它的关闭方法。现在看看我是否可以让它在我的应用程序中工作,而不仅仅是这个例子:

    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();
        }
    }
于 2010-10-08T09:19:04.690 回答
0

你的解决方案对我不起作用。

由于您正在为插件创建一个新进程,因此您可以简单地

addInProcess.Shutdown();

外部进程将关闭

于 2016-03-10T16:38:20.417 回答