2010-10-07 29 views
5

有誰知道如何處理使用System.AddIn創建的AddIns。所有在線示例似乎都顯示瞭如何輕鬆加載和使用外掛程序,但是沒有一個顯示如何在它們還活着時處置它們。我的問題是我在新進程中創建插件,並且這些進程永遠不會收集垃圾,顯然是一個問題。處理使用MAF創建的AddIns(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);  
     } 
    } 
+0

哦,一些額外的信息。我還在我的HostViewAdapter類中保留對合同句柄的引用,這似乎仍然沒有成功。 – 2010-10-07 10:47:47

回答

9

我設法找到上述問題的解決方案,它使用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(); 
     } 
    } 
+2

+1用於報告自己的解決方案。非常符合SO的精神。 – spender 2010-10-08 09:22:16

+2

謝謝,我想我會拯救我的開發夥伴的痛苦 – 2010-10-08 10:19:40

+2

「我以爲我會拯救我的開發夥伴的痛苦」 - 並且System.AddIn有太多的痛苦.... – 2015-03-16 16:32:57

0

您的解決方案並沒有爲我工作。

因爲要創建您的插件一個新的過程中,你可以簡單地

addInProcess.Shutdown(); 

外部進程將關閉