-2

i want kill some process when form is closed

private void Form1_Closed(object sender, System.EventArgs e)
        {
            Process[] processList = Process.GetProcessesByName("notepad");

            if(processList.Length > 0)
            {
                processList[0].Kill();
            }

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Process[] processList = Process.GetProcessesByName("notepad");

            if (processList.Length > 0)
            {
                processList[0].Kill();
            }
        }

like this

but this source doesn't work

i think i need to make some background thread

how to make background thread for check form closed?

8
  • 1
    How does it not work? Is your code getting executed and just not finding anything? It could be that you need to look for notepad.exe instead.
    – Andrew
    Jul 30, 2014 at 14:26
  • What do you need a background thread for? and What does doesnt work mean?
    – Sayse
    Jul 30, 2014 at 14:28
  • The code within the events will work. I tested it. I'm guessing that your events aren't firing. Jul 30, 2014 at 14:29
  • i have low knowledge about c# i want kill notepad.exe when my form is closed so i added above two function but it doesn't work Jul 30, 2014 at 14:29
  • 1
    @AndrewArnold it should be without extension, what he does should work as it is. Jul 30, 2014 at 14:29

3 Answers 3

1

Make sure your event it hooked up correctly. Also some extra input here. You are killing the first instance of notepad found in the list.

What happens if that instance belongs to another user?

You should check the owner of the process to make sure it belongs to the current user.

What happens if the user has multiple instances of notepad opened?

//get our seesion id for current user running the app
int currentUser = Process.GetCurrentProcess().SessionId;

//get a list where process equals notepad and session id is current user
List<Process> currentProcesses = Process.GetProcessesByName("notepad").Where(p => p.SessionId.Equals(currentUser)).ToList();

//used list for .ForEach less code to write
currentProcesses.ForEach(p => p.Kill());
0

There is no reason to create a background thread for what your doing. In fact, if Form1 is your main form invoked by Application.Run(new Form1()), any background threads you create will be terminated when your form closes.

Place a breakpoint inside your event handlers to verify that:

  1. They are indeed executing - that is you have the proper event subscriptions in your constructor:

    Form1.Closing += new EventHandler(Form1_FormClosing); Form1.Closed += new EventHandler(Form1_FormClosed);

  2. The process is being returned by Process.GetProcessesByName("notepad")

Update your post if your still having problems after trying the above.

0

I solve this problem

problem is on killing progress

Process[] localByName = Process.GetProcessesByName("notepad");
                foreach (Process p in localByName)
                {
                    p.Kill();
                }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.