【发布时间】:2010-01-31 18:04:09
【问题描述】:
我已经使用 C# 和 Visual Studio 2008 编写了这个小的 MS Outlook 2003 VSTO 插件。它旨在检查每个正在发送的邮件项目的正文中是否包含“附加”一词,如果找到,然后检查数字的附件。如果该数字为零,则询问用户是否真的要发送消息。它应该像 Gmail 实验室功能一样工作。
奇怪的是它可以工作,但是第一次运行它时,我会暂停,就像邮件项目窗口挂起大约 45 秒一样。一旦超过了这一点,它就会在我打开 Outlook 的其余时间里运行得非常快。如果我关闭 Outlook,那么下次我重新打开它并发送消息时,我将再次等待。
大家有什么想法吗?
这是我的加载项的代码:
namespace OutlookAttacher
{
public partial class ThisAddIn
{
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
this.Application.ItemSend += new Outlook.ApplicationEvents_11_ItemSendEventHandler(Application_ItemSend);
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}
void Application_ItemSend(object Item, ref bool Cancel)
{
if (Item is Microsoft.Office.Interop.Outlook.MailItem)
{
Microsoft.Office.Interop.Outlook.MailItem currentItem = Item as Microsoft.Office.Interop.Outlook.MailItem;
Cancel = true;
if (currentItem.Body.Contains("attach"))
{
if (currentItem.Attachments.Count > 0)
{
Cancel = false;
//MessageBox.Show("This message will be sent now.");
currentItem.Send();
}
else
{
DialogResult ans = MessageBox.Show("This message has no attachments. Are you sure you want to send it?", "OutlookAttacher", MessageBoxButtons.YesNo);
if (ans.Equals(DialogResult.Yes))
{
Cancel = false;
//MessageBox.Show("This message will be sent now.");
currentItem.Send();
}
}
}
else
{
Cancel = false;
//MessageBox.Show("This message will be sent now.");
currentItem.Send();
}
}
}
}
}
也欢迎任何关于改进代码的建议,因为这是我第一次尝试 Outlook 加载项。
更新:我在一台使用了 5 年的戴尔笔记本电脑、2 GB 内存和我不知道的英特尔 CPU 上运行这个。我喜欢添加跟踪/调试它的想法。我将不得不弄清楚如何单步执行代码,这样我才能看到它可能花费的时间最长的地方。谢谢大家!
【问题讨论】:
-
延迟可能只是由加载 .NET CLR 和 VSTO 运行时引起的。你在什么硬件上运行它?
-
不知道怎么做,但是你能放跟踪消息看看哪一行占用了那个时间吗?
-
您能否在类级别声明一行来声明
MailItem类型的变量?我想,这是可能导致暂停的第一行(因此它可能正在加载包含MailItem类的程序集/COM dll。
标签: vsto add-in outlook-2003