同时跟踪多个进程

发布于 2024-11-04 05:54:27 字数 811 浏览 0 评论 0原文


我有一个应用程序(winforms),它将文件下载到用户的临时文件夹,然后打开文件供用户查看内容,当文件关闭时,文件将从临时文件夹中删除。如果我打开一个 .pdf 和一个 .doc,则该应用程序工作正常如果另一个 winword 进程仍在运行,则尝试打开一个 .doc 时会出现问题(无论是由我的应用程序打开还是由用户直接打开) 。

我正在使用以下代码:

_OpenFileProces = System.Diagnostics.Process.Start(TempFileName);
_OpenFileProces.EnableRaisingEvents = true;
_OpenFileProces.Exited += new EventHandler(_OpenFileProces_Exited);

这个代码是为了清除临时值

void _OpenFileProces_Exited(object sender, EventArgs e)
    {
        string s = ((System.Diagnostics.Process)sender).StartInfo.FileName;
        System.IO.File.Delete(s);
    }

似乎正在运行的进程正在停止我自己的..并且由于停止它将删除该文件,或者在尝试删除该文件时会生成错误。
您对我如何打开自己的流程有什么建议吗?问题是我不知道我必须打开什么文件类型(可以是任何类型),我指望 Windows 来选择最好的应用程序。根据我的测试,记事本工作正常,但 winword 和 acrobat 关闭了我的进程。
谢谢

I have an application (winforms) that downloads a file to user's temporary folder, then it opens the file for user to see contents, and when the file is closed, the file gets deleted from temp folder. The application is working ok if I open let's say one .pdf and one .doc The problem appears when trying to open one .doc if another winword process is still runing (doesn't matter if is opened by my app or directly by user).

I'm using the following code:

_OpenFileProces = System.Diagnostics.Process.Start(TempFileName);
_OpenFileProces.EnableRaisingEvents = true;
_OpenFileProces.Exited += new EventHandler(_OpenFileProces_Exited);

and this one to clear temp

void _OpenFileProces_Exited(object sender, EventArgs e)
    {
        string s = ((System.Diagnostics.Process)sender).StartInfo.FileName;
        System.IO.File.Delete(s);
    }

It seems that the running process is stopping my own.. and due to stopping it will delete the file or it will generate an error while trying to delete the file.
Do you have any suggestion how can I open my own process? The thing is I do not know what file type I have to open (it could be anything) and I'm counting on windows to choose the best application. from my test, notepad works ok, but winword and acrobat closes my process.
Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

明媚如初 2024-11-11 05:54:27

我怀疑 Microsoft Word 在这里所做的事情与 Raymond Chen 描述 Windows Shell 所做的事情完全相同 此处

一位客户需要帮助来监控资源管理器窗口的生命周期。

<块引用>

我们想要启动资源管理器的副本来打开特定文件夹,然后等到用户关闭该文件夹后再继续。我们尝试在命令行上使用该文件夹启动资源管理器的副本,然后执行进程句柄上的 WaitForSingleObject,但等待有时会立即完成,而无需等待,我们如何等到用户关闭资源管理器窗口?

这又是一个问题解决一半,另一半却遇到麻烦的情况。

WaitForSingleObject 立即返回的原因是 Explorer 是一个单实例程序(嗯,有限实例)。当您打开资源管理器窗口时,请求将被传递给正在运行的资源管理器副本,并且您启动的资源管理器副本将退出。这就是为什么您的 WaitForSingleObject 立即返回。

在您的情况下,Word 已经在运行,因此当您创建第二 Word 进程并指示它打开文档时,它只是将请求传递给已经运行的 Word 实例,然后退出您立即启动的第二个进程。

这就是当您描述“正在运行的进程正在停止我自己的进程”时所看到的情况。因为第二个实例在您启动后立即关闭,所以会引发 Exited 事件,并且您的代码会告诉它删除该文件!

您敏锐地观察到记事本(与 Word 和 Adob​​e Acrobat 不同)运行得很好。这是因为记事本被设计为多实例应用程序。您可以打开任意数量的记事本副本;它并不关心桌面上是否已经打开了 1 个或 6 个副本。更重要的是,要求 shell 在记事本中打开文本文档实际上会打开记事本应用程序的第二副本,而不是向第一个实例发送请求来打开新文档的新窗口。

I suspect that Microsoft Word is doing exactly the same thing here as Raymond Chen describes the Windows Shell as doing here:

A customer wanted help with monitoring the lifetime of an Explorer window.

"We want to launch a copy of Explorer to open a specific folder, then wait until the user closes the folder before continuing. We tried launching a copy of Explorer with the folder on the command line, then doing a Wait­For­Single­Object on the process handle, but the wait sometimes completes immediately without waiting. How do we wait until the user closes the Explorer window?"

This is another case of solving a problem halfway and then having trouble with the other half.

The reason that Wait­For­Single­Object returns immediately is that Explorer is a single-instance program (well, limited-instance). When you open an Explorer window, the request is handed off to a running copy of Explorer, and the copy of Explorer you launched exits. That's why your Wait­For­Single­Object returns immediately.

In your case, Word is already running, so when you create a second Word process and instruct it to open your document, it simply hands the request off to the instance of Word that is already running, and quits the second process you launched immediately.

That's what you're seeing when you describe that "the running process is stopping my own". Because that second instance gets closed immediately after you launch it, the Exited event is raised and your code tells it to delete the file!

You astutely observe that Notepad (unlike Word and Adobe Acrobat) works just fine. That's because Notepad is designed to be a multiple-instance application. You can open as many copies of Notepad as you want; it doesn't care if there's already 1 or 6 copies open on the desktop. And more importantly, asking the shell to open a text document in Notepad actually opens a second copy of the Notepad application, rather than sending a request to the first instance to open a new window for the new doc.

旧故 2024-11-11 05:54:27

在启动进程之前,您应该将 Process.StartInfo.UseShellExecute 设置为 true 像这样 _OpenFileProces.StartInfo.UseShellExecute = true; 然后它应该可以工作我认为......

You should set the Process.StartInfo.UseShellExecute to true like this _OpenFileProces.StartInfo.UseShellExecute = true; before starting the process and then it should work I think...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文