加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 08:31:07 所属栏目:百科 来源:网络整理
导读:有谁知道如何处理使用System.AddIn创建的AddIns.在线的所有示例似乎都显示了如何轻松加载和使用插件,但没有一个显示如何在它们活着时处置它们.我的问题是我在新进程中创建插件,这些进程永远不会被垃圾收集,显然是一个问题. 下面是一些说明我的问题的示例代码
有谁知道如何处理使用System.AddIn创建的AddIns.在线的所有示例似乎都显示了如何轻松加载和使用插件,但没有一个显示如何在它们活着时处置它们.我的问题是我在新进程中创建插件,这些进程永远不会被垃圾收集,显然是一个问题.

下面是一些说明我的问题的示例代码.假设用户从不退出此应用程序,而是创建许多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);    
        }
    }

解决方法

我设法找到了上述问题的解决方案,它正在使用AddInController类和它的shutdown方法.现在看看我是否可以在我的应用程序中使用它,而不仅仅是这个例子:
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();
        }
    }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读