同时跟踪多个进程
我有一个应用程序(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我怀疑 Microsoft Word 在这里所做的事情与 Raymond Chen 描述 Windows Shell 所做的事情完全相同 此处:
在您的情况下,Word 已经在运行,因此当您创建第二 Word 进程并指示它打开文档时,它只是将请求传递给已经运行的 Word 实例,然后退出您立即启动的第二个进程。
这就是当您描述“正在运行的进程正在停止我自己的进程”时所看到的情况。因为第二个实例在您启动后立即关闭,所以会引发
Exited
事件,并且您的代码会告诉它删除该文件!您敏锐地观察到记事本(与 Word 和 Adobe 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:
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.
在启动进程之前,您应该将 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...