DanielVest
DanielVest

Reputation: 823

How can i kill two processes?

I have in my Form1 button click event this line:

Process.Start(Path.GetFullPath(zippedFileDirectory));

Its just openning the location of this directory. Then I have another line:

Process.Start(Path.GetFullPath(temp));

If I clicked the first button and then the second button I will have two windows opened each of a process.

Now im closing my program so in the Form1 closing event I want to kill this two processes.

In the else part what do I do ?

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    if (MessageBox.Show("Are you Sure you want to Exit. Click Yes to Confirm and No to continue", "WinForm",
                         MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
    {
         e.Cancel = true;
    }
    else
    {

    }
}

Edit:

I did now in my Form1 top added:

private Process zipFileDirectoryProcess;

Then in the bottom inside the method I did:

private void Compress()
{
    string zipFileName = "Diagnosis_Files.zip";
    string source = contentDirectory;
    string output = zippedFileDirectory;
    string programFilesX86 = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFilesX86) 
                                                     + "\\Diagnostic Tool\\7z.dll";
    if (File.Exists(programFilesX86))
    {
        SevenZipExtractor.SetLibraryPath(programFilesX86);
    }
    string programFiles = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFiles) 
                                 + "\\Diagnostic Tool\\7z.dll";
    if (File.Exists(programFiles))
    {
        SevenZipExtractor.SetLibraryPath(programFiles);
    }
    SevenZipCompressor compressor = new SevenZipCompressor();
    compressor.ArchiveFormat = OutArchiveFormat.Zip;
    compressor.CompressionMode = CompressionMode.Create;
    compressor.TempFolderPath = System.IO.Path.GetTempPath();
    string t = Path.Combine(output, zipFileName);
    compressor.CompressDirectory(source, t);
    zipFileDirectoryProcess.Start(Path.GetFullPath(zippedFileDirectory));
    this.TopMost = true;
}

I added the line:

zipFileDirectoryProcess.Start(Path.GetFullPath(zippedFileDirectory));

But im getting an error on this line:

Error 1 Member 'System.Diagnostics.Process.Start(string)' cannot be accessed with an instance reference; qualify it with a type name instead

And before I added this line I had this line:

Process.Start(Path.GetFullPath(zippedFileDirectory));

And it worked good so why now I have this error ?

Upvotes: 2

Views: 278

Answers (2)

King King
King King

Reputation: 63327

Process p1 = Process.Start(Path.GetFullPath(zippedFileDirectory));
Process p2 = Process.Start(Path.GetFullPath(temp));
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (MessageBox.Show("Are you Sure you want to Exit. Click Yes to Confirm and No to continue", "WinForm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
        {
            e.Cancel = true;
        }
        else
        {
           p1.Kill();
           p2.Kill();
        }
    }

UPDATE

For your added question, you can't call the Start() method on an instance of Process because:

  1. There is no overriding method (instance method) of Start() taking 1 argument of string. There is only 1 method without any argument Start().
  2. The IDE says you should call the Start() on a type name that means you should call Process.Start(string ....) whereas Process is a type name. This Start(string...) is a static method which can only be called on a type name.

Upvotes: 2

Vaughan Hilts
Vaughan Hilts

Reputation: 2879

Process.Start returns a process object. You can store this somewhere, and then call Kill() on it if they select yes. So, something like:

Process _proc;

_proc = Process.Start("path);
_proc.Kill();

Upvotes: 0

Related Questions