监控 Outlook 启动/关闭
您将在下文中找到一个类的摘录,旨在在 Outlook 启动时在 Outlook 上下文菜单中添加一个按钮。 当 Outlook 未启动时,启动事件观察程序将被激活以检测 Outlook 启动。 Close EventWatcher 还配备了允许在 Outlook 关闭时清理资源的功能。
我在管理模式下运行此代码,但应用程序随机崩溃。如果我禁用 EventWatchers 周围的所有代码,它就会稳定。我找不到这些崩溃的根源,你能帮我吗? (请原谅我文中的法国评论)。
入口点是 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();
}
}
You will find hereafter an extract of a class aiming at adding a button in Outlook context menu if Outlook is launched.
When outlook is not launched, a Start EventWatcher is armed to detect Outlook startup.
A Close EventWatcher is also armed to allow ressources clean-up when Outlook is closed.
I run this code in Admin mode but the application randomly crashes. If I disable all the code around EventWatchers it's stable. I can't find the origin of these crashes, can you help me ? (Forgive me for the french comments in the text).
The entry point is the tryHook method.
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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经简化了代码并将其放入线程中,它似乎可以工作。
然而,有一个巨大的缺点:Oulook 和程序都必须在管理员模式下运行。
我对限制较少的新提议持开放态度。
这是线程的创建:
和新类:
I've simplified the code and put it in a thread and it seems to work.
Nevertheless there is a huge drawback: Oulook and the program shall both run in Admin mode.
I am open to new propositions less restrictive.
Here is the thread creation:
And the new class: