【发布时间】:2011-03-24 14:23:32
【问题描述】:
如果启动 Outlook,您将在下文中找到旨在在 Outlook 上下文菜单中添加按钮的类的摘录。 当 Outlook 未启动时,启动 EventWatcher 将被启用以检测 Outlook 启动。 关闭 EventWatcher 也可以在 Outlook 关闭时进行资源清理。
我在管理员模式下运行此代码,但应用程序随机崩溃。如果我禁用 EventWatchers 周围的所有代码,它是稳定的。我找不到这些崩溃的根源,你能帮我吗? (请见谅文中的法国cmets)。
入口点是tryHook方法。
public class OutlookIF
{
// Attributs métiers
private Outlook.Application outlook = null;
// Process watch
private ManagementEventWatcher startWatch =
new ManagementEventWatcher(new WqlEventQuery("SELECT * FROM Win32_ProcessStartTrace WHERE ProcessName = 'OUTLOOK.EXE'"));
private ManagementEventWatcher stopWatch =
new ManagementEventWatcher(new WqlEventQuery("SELECT * FROM Win32_ProcessStopTrace WHERE ProcessName = 'OUTLOOK.EXE'"));
// Gestion du singleton
private static OutlookIF v_instance = null;
public static OutlookIF Instance
{
get
{
if (v_instance == null)
v_instance = new OutlookIF();
return v_instance;
}
}
private OutlookIF()
{
// Récupération des évènements EventArrived
startWatch.EventArrived += new EventArrivedEventHandler(startWatch_EventArrived);
stopWatch.EventArrived += new EventArrivedEventHandler(stopWatch_EventArrived);
}
// Vérification de la présence d'un process Outlook running
private bool outlookIsLaunched = (Process.GetProcessesByName("outlook").Count() > 0);
//Tentative de connexion à Outlook
public void tryHook()
{
if (this.outlookIsLaunched)
{
this.hookOutlook();
this.stopWatch.Start();
}
else
this.startWatch.Start();
}
// Ajout du menu contextuel à Outlook
private void hookOutlook()
{
// Création de l'objet application
if (this.outlookIsLaunched)
this.outlook = Marshal.GetActiveObject("Outlook.Application") as Outlook.Application;
else
this.outlook = new Outlook.ApplicationClass();
// Création de l'entrée dans le menu contextuel
this.outlook.ItemContextMenuDisplay += new Outlook.ApplicationEvents_11_ItemContextMenuDisplayEventHandler(addEntrytoContextMenu);
}
// Nettoyage des objets
private void clean()
{
Marshal.FinalReleaseComObject(this.outlook);
this.outlook = null;
}
private void startWatch_EventArrived(object sender, EventArrivedEventArgs e)
{
// Arrêt de l'écoute de l'ouverture
this.startWatch.Stop();
TrayIcon.afficheMessage("ProcessWatch", "Ouverture d'Outlook");
this.hookOutlook();
// Démarrage de l'écoute de la fermeture
this.stopWatch.Start();
}
private void stopWatch_EventArrived(object sender, EventArrivedEventArgs e)
{
// Arrêt de l'écoute de la fermeture
this.stopWatch.Stop();
TrayIcon.afficheMessage("ProcessWatch", "Fermeture d'Outlook");
this.clean();
// Démarrage de l'écoute de l'ouverture
this.startWatch.Start();
}
}
【问题讨论】:
标签: c# wmi outlook-addin