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

发布于 2024-09-26 03:34:20 字数 1014 浏览 5 评论 0原文

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

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故人爱我别走 2024-10-03 03:34:20

我设法找到了上述问题的解决方案,它利用了 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();
        }
    }

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();
        }
    }
厌味 2024-10-03 03:34:20

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

由于您正在为您的外接程序创建一个新进程,因此您可以简单地

addInProcess.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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文