C# 如何在Visual Studio Tools For Office(VSTO)2010 Outlook加载项完成后进行清理
Microsoft提供了一个简单的VSTO 2010 Outlook加载项演练,它完美地说明了我在支持的更复杂加载项中看到的一个问题。以下是演练的链接: 下面是我复制到C#VSTO 2010 Outlook加载项项目中的演练代码:C# 如何在Visual Studio Tools For Office(VSTO)2010 Outlook加载项完成后进行清理,c#,outlook,garbage-collection,vsto,C#,Outlook,Garbage Collection,Vsto,Microsoft提供了一个简单的VSTO 2010 Outlook加载项演练,它完美地说明了我在支持的更复杂加载项中看到的一个问题。以下是演练的链接: 下面是我复制到C#VSTO 2010 Outlook加载项项目中的演练代码: using Outlook = Microsoft.Office.Interop.Outlook; using Office = Microsoft.Office.Core; namespace FirstOutlookAddIn { public par
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;
namespace FirstOutlookAddIn
{
public partial class ThisAddIn
{
private Outlook.Inspectors inspectors;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
inspectors = this.Application.Inspectors;
inspectors.NewInspector +=
new Microsoft.Office.Interop.Outlook
.InspectorsEvents_NewInspectorEventHandler(Inspectors_NewInspector);
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}
void Inspectors_NewInspector(Microsoft.Office.Interop.Outlook.Inspector Inspector)
{
Outlook.MailItem mailItem = Inspector.CurrentItem as Outlook.MailItem;
if (mailItem != null)
{
if (mailItem.EntryID == null)
{
mailItem.Subject = "Added Text";
mailItem.Body = "Added Text to Body";
}
}
}
#region VSTO generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}
#endregion
}
}
我也一直在尝试,但到目前为止运气不佳。有人能提供一些关于如何解决这个问题的建议吗?如果要从堆中删除未使用的COM对象,需要调用GC两次。例如:
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
但是更好的方法是在使用完Outlook对象后使用它来释放它。如果外接程序试图枚举存储在Microsoft Exchange服务器上的集合中超过256个Outlook项目,这一点尤为重要。然后在Visual Basic中将变量设置为Nothing(在C#中为null),以释放对对象的引用。您可以在MSDN的文章中了解更多信息。您是否必须取消订阅活动?看看这个
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();