简体   繁体   中英

C# - Kill process doesn't work properly

I developing some parts of WPF application. I'm creating multiple user control for it. So, one of my user control, i open IExplore.exe with a url. After close that user control, i close that process. On my system seems working properly, but client's machines that using my user controls seems not working that way i want. Closing the Iexplore.exe thats right but when i look into "Task Manager" i seeing too many iexplore.exe . Some of that Iexplore.exe cannot be close, so kill all process with this name option not a option for me :)

This is my class for start process.

class ses_process
    {
        static Process p = new Process();
        private Process Proc;

        public static bool start(string url)
        {

            p = Process.Start("IEXPLORE.EXE", "-nomerge "+url);
            //startInfo.WindowStyle = ProcessWindowStyle.Maximized;
            return true;
        }
        public static bool stop()
        {
            p = Process.GetProcessById(p.Id);

            bool return_deger = p.CloseMainWindow();
            return return_deger;
        }
        public Process proc
        {
            get { return Proc; }
            set
            {
                if (Proc == null)
                {
                    Proc = p;
                }
            }
        }

    }

It's looking like this on Task Manager;

iexplore.exe                 16524 Console                   10     20.268 K
iexplore.exe                 22572 Console                   10     40.636 K
iexplore.exe                  2356 Console                   10    109.452 K
calc.exe                     20572 Console                   10     11.208 K
wuauclt.exe                  11716 Console                   10      1.092 K
RuntimeBroker.exe             6096 Console                   10      3.180 K
iexplore.exe                 10660 Console                   10     16.536 K
iexplore.exe                 18272 Console                   10     71.972 K
iexplore.exe                 20996 Console                   10     15.004 K
iexplore.exe                 14188 Console                   10      2.080 K
iexplore.exe                 12664 Console                   10     15.120 K
iexplore.exe                  5612 Console                   10     27.660 K
iexplore.exe                 18772 Console                   10     15.572 K
iexplore.exe                 22568 Console                   10     35.944 K
iexplore.exe                 21796 Console                   10     14.852 K
iexplore.exe                 10524 Console                   10     19.100 K
iexplore.exe                 13984 Console                   10     14.808 K
iexplore.exe                 21088 Console                   10     19.664 K
iexplore.exe                 10856 Console                   10     14.008 K
iexplore.exe                  1048 Console                   10     12.652 K
iexplore.exe                 22236 Console                   10     15.428 K
iexplore.exe                 15584 Console                   10     24.204 K
iexplore.exe                 16248 Console                   10      6.116 K
iexplore.exe                  9684 Console                   10      3.064 K
iexplore.exe                   752 Console                   10     14.480 K
iexplore.exe                 12680 Console                   10     19.004 K
iexplore.exe                  5772 Console                   10     14.064 K
iexplore.exe                 17868 Console                   10     18.872 K
iexplore.exe                  9272 Console                   10      5.644 K
iexplore.exe                 14216 Console                   10      3.332 K
iexplore.exe                  6060 Console                   10     11.820 K
iexplore.exe                 21352 Console                   10     12.592 K
iexplore.exe                 19604 Console                   10     15.392 K
iexplore.exe                 16636 Console                   10     23.916 K
iexplore.exe                 12584 Console                   10     14.796 K
iexplore.exe                  2848 Console                   10     21.884 K
iexplore.exe                 13696 Console                   10      5.608 K
iexplore.exe                 11720 Console                   10      3.296 K
SearchProtocolHost.exe       20896 Console                   10      2.404 K

You can try this:

Process[] processes = Process.GetProcessesByName("your_process");
foreach (Process process in processes)
{
    process.Kill();
    process.WaitForExit();
}

or like this:

Process process = Process.GetProcessById(12345678);
process.Kill();

You can of course add WaitForExit method if you want. Kill method is async, so if you want to know whether process was killed already you should wait for it, if not - just call Kill method. For more details look here

EDIT: If you started the process by yourself, you should use this code: Basically Process implements IDisposable interface, you should call it by using syntax for example:

using(Process proc = CreateProcess())
{
    StartProcess(proc);
    if (!proc.WaitForExit(timeout))
    {
        proc.Kill();
    }
}

Look at this and this answer.

And once again, if you want to kill it in the middle of processing - just use Kill method. You don't have to have id, name or whatever. You have reference to that process, right? that's enough to kill it

Have you tried using Process.Kill() instead? I'm not sure that CloseMainWindow() will do the trick. I would also suggest calling Dispose() on any Process objects you're done with and removing the static from p as if may be causing you to leak Process objects which could produce this behavior.

Dispose() may call kill Kill() (I'm not sure) but I doubt it. It's a good practice to call Dispose() on all objects that implement IDisposable because they use some type of resource or another that should be released. That's what a using() {} block is for. However, this is all beside the point.

I think removing the static modifier from p and calling Kill() instead of CloseMainWindow() will fix your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM