Process.Exited 并不总是触发
如果我运行以下代码:
Process myProcess = new System.Diagnostics.Process();
myProcess.StartInfo.FileName = "notepad.exe";
myProcess.EnableRaisingEvents = true;
myProcess.Exited += new System.EventHandler(Process_OnExit);
myProcess.Start();
public static void Process_OnExit(object sender, EventArgs e)
{
// Delete the file on exit
}
退出记事本时会引发该事件。如果我尝试相同的代码,但我改为启动图像:
Process myProcess = new System.Diagnostics.Process();
myProcess.StartInfo.FileName = @"C:\Users\Public\Pictures\Sample Pictures\Desert.jpg";
myProcess.EnableRaisingEvents = true;
myProcess.Exited += new System.EventHandler(Process_OnExit);
myProcess.Start();
public static void Process_OnExit(object sender, EventArgs e)
{
// Delete the file on exit
}
该事件永远不会被触发。是因为加载图像的进程永远不会关闭吗?
更新:启动的进程并不总是图像。它可以是任何内容(pdf、word 文档等)。也许是我的做法不对。用户退出进程后是否有其他方法删除该文件?
谢谢
If I run the following code :
Process myProcess = new System.Diagnostics.Process();
myProcess.StartInfo.FileName = "notepad.exe";
myProcess.EnableRaisingEvents = true;
myProcess.Exited += new System.EventHandler(Process_OnExit);
myProcess.Start();
public static void Process_OnExit(object sender, EventArgs e)
{
// Delete the file on exit
}
The event is raised when I exit notepad. If I try the same code, but I start an image instead :
Process myProcess = new System.Diagnostics.Process();
myProcess.StartInfo.FileName = @"C:\Users\Public\Pictures\Sample Pictures\Desert.jpg";
myProcess.EnableRaisingEvents = true;
myProcess.Exited += new System.EventHandler(Process_OnExit);
myProcess.Start();
public static void Process_OnExit(object sender, EventArgs e)
{
// Delete the file on exit
}
The event is never fired. Is it because the process that loads the image is never closed ?
UPDATE : The process to start is not always an Image. It can be anything (pdf, word document, etc). Maybe my approach isn't right. Is there any other way to delete the file after the user exited the process ?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您应该启用该流程的引发事件。
you should enable raising events for the process.
我会使用临时文件。有一些函数可以创建临时文件...
我猜,由于缺少进程本身,您的事件没有触发。您可以尝试使用 shell 来“启动”相关文档,但不能保证所有类型的文件都会有一个处理程序。
I would use a temp file. There are functions to create a temp file...
Your event is not firing due to the lack of the process itself, I guess. You can try to use the shell to "start" the document in question but nothing guarantees that there will be a handler for all types of files.
对于 Windows 媒体播放器,请尝试以下代码
对于 Windows 图片查看器,请尝试此操作
现在,两者都会在 Windows 7 中提供退出事件
For windows media player try the following code
For windows picture viewer try this
Now both will give your exited event in Windows 7
您正在使用 Windows 中的默认图像查看器,因为图像文件不可执行。我更改了代码以使用 XP 默认值,并且运行良好。
You are using the default image viewer in windows since an image file is not executable. I changed the code to use the XP default and it worked fine.