简体   繁体   English

处理使用MAF创建的AddIns(System.AddIn)

[英]Dispose of AddIns created using MAF (System.AddIn)

Does anyone know how to dispose of AddIns created using System.AddIn. 有谁知道如何处理使用System.AddIn创建的AddIns。 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. 假设用户从不退出此应用程序,而是创建许多ICalculator实例。 How do these addIn processes ever get disposed of? 这些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);    
        }
    }

I managed to find a solution to the above problem, it's making use of the AddInController class and it's shutdown method. 我设法找到了上述问题的解决方案,它正在使用AddInController类和它的shutdown方法。 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 外部进程将关闭

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM