Click here to Skip to main content
15,914,074 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
my application creates some processess, i want to kill them when user click on red cancel button of my form. so i write the code in Form1_FormClosing() but no result. The code i wrote in the function is correct, i have checked it.

C#
private void Form1_FormClosing(Object sender, FormClosingEventArgs e)
{

    int i = 0;
    while (i < p.MyProcess.Count)
    {
        if (!p.MyProcess[i].HasExited)
        {
            p.MyProcess[i].Kill();
            p.MyProcess[i].Close();

        }
        i++;
    }
    p.MyProcess.Clear();
}
Posted
Updated 20-Nov-11 6:46am
v2

1 solution

In your Button.Click Event Handler?
Or perhaps in a Method you call from your Event Handler, depending on your taste.
C#
private void button_Click(Object sender, EventArgs e)
{
    // Code goes here.
}
Or, if you like:
C#
private void button_Click(Object sender, EventArgs e)
{
    KillProcesses();
}

private void KillProcesses()
{
    // Code goes here.
}
If you need this exact piece of code in multiple Forms or Classes, consider placing it in a seperate Class and making it static (assuming you don't need instance fields to kill processes).
 
Share this answer
 
Comments
Sweety Khan 19-Nov-11 12:51pm    
yeh i know but this button is a button which i will place on my form from toolbox, but i want to code for the red cancel button of my dialog box
Sander Rossel 19-Nov-11 13:19pm    
You didn't mention that dialog box in your question.
However, a DialogBox should not have any functionality. Like, for example, MessageBox.Show("Test here.") is a Function that returns a DialogResult you should also program your own DialogBox (or are you using MessageBox?).
In that case, you would get something like this:
if (MessageBox.Show("Are you sure you want to kill the processes?") == DialogResult.Yes)
{
// Kill the processes.
}
Sweety Khan 19-Nov-11 13:23pm    
dialog box means my main form's red cancel button which is by default with the form
Uday P.Singh 19-Nov-11 13:34pm    
what do you mean by red cancel button which is by default with the form? are you talking about the red cross button for closing?
Sweety Khan 19-Nov-11 18:57pm    
yes exactly

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900