Process.Exited 并不总是触发

发布于 2024-08-19 10:42:57 字数 918 浏览 3 评论 0原文

如果我运行以下代码:

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 技术交流群。

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

发布评论

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

评论(4

往事随风而去 2024-08-26 10:42:57

您应该启用该流程的引发事件。

process_name.EnableRaisingEvents = true;

you should enable raising events for the process.

process_name.EnableRaisingEvents = true;
诺曦 2024-08-26 10:42:57

我会使用临时文件。有一些函数可以创建临时文件...

我猜,由于缺少进程本身,您的事件没有触发。您可以尝试使用 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.

何以畏孤独 2024-08-26 10:42:57

对于 Windows 媒体播放器,请尝试以下代码

 myProcess.StartInfo.FileName = "wmplayer";
 myProcess.StartInfo.Arguments = "yourfilename";

对于 Windows 图片查看器,请尝试此操作

 myProcess.StartInfo.FileName = @"rundll32.exe";
 myProcess.StartInfo.Arguments = @"C:\Windows\System32\shimgvw.dll,ImageView_Fullscreen " + yourfilepath;

现在,两者都会在 Windows 7 中提供退出事件

For windows media player try the following code

 myProcess.StartInfo.FileName = "wmplayer";
 myProcess.StartInfo.Arguments = "yourfilename";

For windows picture viewer try this

 myProcess.StartInfo.FileName = @"rundll32.exe";
 myProcess.StartInfo.Arguments = @"C:\Windows\System32\shimgvw.dll,ImageView_Fullscreen " + yourfilepath;

Now both will give your exited event in Windows 7

离笑几人歌 2024-08-26 10:42:57

您正在使用 Windows 中的默认图像查看器,因为图像文件不可执行。我更改了代码以使用 XP 默认值,并且运行良好。

class Program
{
    static void Main(string[] args)
    {
        Process myProcess = new System.Diagnostics.Process(); 
        myProcess.StartInfo.FileName = @"rundll32.exe"; 
        myProcess.EnableRaisingEvents = true;
        myProcess.StartInfo.Arguments = @"C:\winnt\System32\shimgvw.dll,ImageView_Fullscreen c:\leaf.jpg";
        myProcess.Exited += new System.EventHandler(Process_OnExit); 
        myProcess.Start();
        Console.Read();



    }
    public static void Process_OnExit(object sender, EventArgs e)
    {
        Console.WriteLine("called");
        Console.Read();
    } 


}

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.

class Program
{
    static void Main(string[] args)
    {
        Process myProcess = new System.Diagnostics.Process(); 
        myProcess.StartInfo.FileName = @"rundll32.exe"; 
        myProcess.EnableRaisingEvents = true;
        myProcess.StartInfo.Arguments = @"C:\winnt\System32\shimgvw.dll,ImageView_Fullscreen c:\leaf.jpg";
        myProcess.Exited += new System.EventHandler(Process_OnExit); 
        myProcess.Start();
        Console.Read();



    }
    public static void Process_OnExit(object sender, EventArgs e)
    {
        Console.WriteLine("called");
        Console.Read();
    } 


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