0

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
Will Vousden
  • 32,488
  • 9
  • 84
  • 95
orhun.begendi
  • 937
  • 3
  • 16
  • 31
  • You may need permission to terminate the process. – Lloyd Jun 17 '15 at 12:32
  • Could it be possible that plugins/extentions in IE have their own process like chrome? – Satbir Kira Jun 17 '15 at 12:34
  • Are you sure you're only starting it once? Are there many users on the same machine all with IE open at the same time? – sirdank Jun 17 '15 at 12:42
  • `p = new Process();` Assigns an empty process which you never use. `p = Process.GetProcessById(p.Id);` Overwrites p with the process found using p.Id, why not just use p? Also as @sirdank points out. If you call start more than once, you lose your previous reference to p and can't close the window anymore – James Jun 17 '15 at 12:44
  • on debug i can see p.Id was ok on my system. close it works fine but on client didnt close at all. is there any problem for get processbyID ? Could be getting wrong id ? What is the best way to close/kill process that i started? – orhun.begendi Jun 17 '15 at 14:24
  • @orhun.begendi Look at EDIT in my answer. If you want to close process that you started (it has finished) - best way would be disposing it (close is called then). You if want to kill it, you don't need id or name when you have reference (but you need to keep that reference of course) – voytek Jun 18 '15 at 21:47

2 Answers2

3

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

Community
  • 1
  • 1
voytek
  • 2,202
  • 3
  • 28
  • 44
  • is it kill all process with that name(Iexplore.exe) ? I dont want to kill all process with this name because my client using some page on Iexplore.exe for check e-mail etc., i need kill only that i open – orhun.begendi Jun 17 '15 at 13:47
  • @orhun.begendi Then you have to get rid of foreach loop and get process by id. Using this: Process.GetProcessById – voytek Jun 17 '15 at 13:49
0

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.

sirdank
  • 3,351
  • 3
  • 25
  • 58
  • what is the difference between dispose and kill ? And how to kill it true one ? – orhun.begendi Jun 17 '15 at 14:25
  • @orhun.begendi I edited my answer to include some clarification regarding `Dispose()` vs `Kill()`. I still think your biggest problem is `CloseMainWindow()` and the `static` modifier. – sirdank Jun 17 '15 at 14:56