Why is the Process Exited event constantly triggered automatically?

At the top of Form1, I did:

private Process zipFileDirectoryProcess; 

In the constructor, I did:

 zipFileDirectoryProcess = new Process(); zipFileDirectoryProcess.StartInfo.FileName = "explorer.exe"; zipFileDirectoryProcess.StartInfo.CreateNoWindow = true; zipFileDirectoryProcess.EnableRaisingEvents = true; zipFileDirectoryProcess.Exited += new EventHandler(zipFileDirectoryProcess_Exited); 

Then I have a method that I call from the button click event:

 private void Compress() { zipFileDirectoryProcess.StartInfo.Arguments = zipFileDirectoryProcess.StartInfo.Arguments = "/select," + Path.GetFullPath(t); zipFileDirectoryProcess.Start(); zipFileDirectoryProcess.WaitForExit(); this.TopMost = true; } 

And then at the bottom of the Exited event:

 private void zipFileDirectoryProcess_Exited(object sender, EventArgs e) { this.BeginInvoke(new MethodInvoker(delegate() { this.TopMost = false; })); } 

What I wanted to do was only when I close the process window after it was started in this method only if the window / process was closed and then the Exited event occurs.

The problem is that after the process started in 2-3 seconds, it automatically goes to the Exited event.

How can i fix this? Tried examples can not understand. Tried to add this line:

 zipFileDirectoryProcess.WaitForExit(); 

But no effect.

+4
source share
2 answers
  zipFileDirectoryProcess.StartInfo.FileName = "explorer.exe"; 

Trying to start Windows Explorer again when it is already running, and it always works, will have disappointing results. This is a "difficult" process, and it is intentionally trying to minimize the number of running copies. Otherwise, it is called a "single-instance application." For example, such Microsoft Office programs are single-instance applications.

So what really happens is that explorer.exe does start, but sees that another instance is already running. And uses the interop of the process to ask that the first instance completed the task that you asked for. Since you did not ask him to do anything, you just get another window displayed by the first instance. The one you started immediately leaves, he has nothing more to do.

So yes, you will see that the Exit event is triggered if you do nothing. Exactly telling you that the explorer.exe process that you started actually stopped working. Easy to see on the tab Processmgr.exe Processs btw. Waiting for this window to close will never work; it is displayed by the original explorer.exe instance.

This will not work the way you hope it works. What you are actually trying to do is not entirely obvious, but you can guess. Creating a ZIP archive is not complicated, there are excellent libraries available for C # to complete the task, you do not need to ask another program to do this for you. DotNetZip and SharpZipLib are very popular. Finally, it was added to .NET, and also in version 4.5, Microsoft finally overcame Stacker's lost lawsuit about time. If you really want another program to do this for you, use a 7-zip console zipper.

+5
source

To show the output folder in Windows Explorer for the user, simply do this:

 Process.Start("explorer.exe", OutputDir); 
0
source

Source: https://habr.com/ru/post/1495227/


All Articles