如何检测用户是否正在访问我已启动的 Outlook 进程?
我有一个 C# 应用程序,在其中我通过 Office PIA 访问 Outlook 以便解析约会。在我的应用程序中,当我创建 Outlook 对象时,会在任务管理器中创建一个新的 Outlook 进程。当我关闭应用程序时,我也退出了 Outlook 对象。这会关闭我的任务管理器中的 Outlook 进程。
我的问题是,当用户也与 Outlook 交互时。具体来说,如果用户打开了 Outlook,当我的程序关闭时,我的程序也会关闭该用户的 Outlook 程序。尝试手动和使用我的程序打开 Outlook,同时观察任务管理器,似乎只有一个 Outlook 实例可以同时运行,然后用户和我的程序都访问同一个实例,所以当我关闭我的程序中的 Outlook,我还关闭了用户的程序。
奇怪的是,相反的情况却并非如此。如果用户关闭outlook,当我的程序运行时,他的窗口就会消失,但是outlook进程不会关闭,我的程序仍然可以继续。就好像窗口只是 Outlook 进程的客户端,尽管窗口没有单独的进程。有没有一种方法可以让我看到,我的程序是否是唯一使用 Outlook 进程的程序,或者是否有任何“用户窗口客户端”打开,这样我就可以避免在 Outlook 仍在使用时关闭它?
I have a C# application, in which I access outlook through the Office PIAs in order to parse appointments. In my application, when I create my outlook object, a new outlook process is created in the task manager. When I close my application, I also quit the outlook object. This shuts down the outlook process in my task manager.
My problem is, when the user is also interacting with outlook. Specifically, if the user has outlook opened, when my program closes, my program also closes that user's outlook program. Experimenting with opening outlook both manually and with my program, while watching the task manager, it seems that only one instance of outlook can ever be running at the same time, and both the user and my program then accesses that same instance, so when I close outlook from my program, I also shut down the user's program.
Strangely enough, the reverse is not the case. If the user shuts down outlook, while my program runs, his window will disappear, but the outlook process will not shut down, and my program can still continue. It is as if the window is only a client of the outlook process, although there is no separate process for the window. Is there a way for me to see, if my program is the only one using the outlook process, or if there are any "user window clients" open, so I can avoid shutting down outlook, when it is still in use?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我自己找到了解决方案。我发现 Outlook 会跟踪有多少浏览器打开它。达到零资源管理器不会导致进程关闭,但它表明没有用户打开任何正常的 Outlook 窗口,因此可以安全地关闭该进程。在我的项目中,我有一个访问 Outlook 的类。我的解决方案是在该类的析构函数中测试零资源管理器(以下代码假设“using Outlook = Microsoft.Office.Interop.Outlook;”):
当然,您必须小心关闭在您的类中使用的所有资源管理器代码,否则解决方案将不起作用,例如:
I found a solution myself. I turns out that outlook keeps track of how many explorers are open on it. Reaching zero explorers does not cause a shutdown of the process, but it indicates that no user has any normal outlook window open, so the process can be safely shut down. In my project I have a single class which accesses outlook. My solution is to test for zero explorers in the destructor of that class (the following code assumes "using Outlook = Microsoft.Office.Interop.Outlook;"):
Of course, you have to be careful to close all explorers you use in your code, or the solution will not work, e.g.: