Why does the Process Exited event automatically increase all the time?

advertisements

In 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 i call it from a 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 in the bottom the Exited event:

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

What i wanted to do is only when i close the process window after started it in the method only if closed the window/process then do the Exited event.

The problem is that once the process started after 2-3 seconds its jumping automatic to the Exited event.

How can i fix it ? Tried examples cant figure out. Tried to add this line:

zipFileDirectoryProcess.WaitForExit();

But no effect.


   zipFileDirectoryProcess.StartInfo.FileName = "explorer.exe";

Trying to start Windows Explorer again when it is already running, and it is always running, will have a disappointing outcome. It is a "heavy" process and it intentionally tries the minimize the number of running copies. Otherwise known as a "single-instance app". There are lots like that, the Microsoft Office programs are single instance apps for example.

So what really happens is that explorer.exe actually starts up, but sees that another instance is already running. And uses process interop to ask that first instance to do the job that you asked it to do. Since you didn't ask it to do anything, you just get another window, displayed by the first instance. The one that you started immediately quits, it doesn't have anything else to do.

So, yes, you'll see that the Exited event fires without you doing anything. Accurately telling you that the explorer.exe process you started did in fact quit. Easy to see in the Taskmgr.exe Processes tab btw. Waiting for that window to be closed is never going to work, it is displayed by the original instance of explorer.exe.

This will just not work the way you hope it will work. What you are actually trying to do is not quite obvious but can be guessed at. Creating a ZIP archive is not difficult, there are excellent libraries available for C# to get the job done, no point in asking another program to do it for you. DotNetZip and SharpZipLib are very popular. It got finally added to .NET as well in version 4.5, Microsoft finally getting over the lost Stacker lawsuit, about time. If you really, really want another program to do it for you then use a console mode zipper like 7-zip.