locked
how i can close other program or process with C# code RRS feed

  • Question

  • i have a process in task manager


    i need - closed this process


    then run my application

    Tuesday, November 15, 2011 6:47 PM

Answers

  • Hi,

    You can use the methods Process.Kill or Process.CloseMainWindow to stop a process. If the app has no GUI then just call Kill, otherwise a well-designed GUI application should respond to the CloseMainWindow method and allow the user to save his work before the application closes. Calling Kill will immediately stop the process and could cause a loss of work.

    Here is a code sample for killing MS Paint:

    public static void KillPaint()
            {
                System.Diagnostics.Process[] procs = null;
    
                try
                {
                    procs = Process.GetProcessesByName("mspaint");
    
                    Process mspaintProc = procs[0];
    
                    if (!mspaintProc.HasExited)
                    {
                        mspaintProc.Kill();
                    }
                }
                finally
                {
                    if (procs != null)
                    {
                        foreach (Process p in procs)
                        {
                            p.Dispose();
                        }
                    }
                }
            }
    

    HTH


    -Scosby
    Microsoft Community Contributor
    Tuesday, November 15, 2011 9:53 PM
  • The code shows already how to end a process. So you need to know how to start some other process? The class Process can be used. But maybe you want to give us more details about your requirements, because you can set a lot of different things with Process.

    Process.Start("C:\Somewhere\SomeApp.exe");
    will start SomeApp.exe in c:\Somewhere.

    With kind regards,

    Konrad

    Wednesday, November 16, 2011 9:22 PM

All replies

  • Hi,

    You can use the methods Process.Kill or Process.CloseMainWindow to stop a process. If the app has no GUI then just call Kill, otherwise a well-designed GUI application should respond to the CloseMainWindow method and allow the user to save his work before the application closes. Calling Kill will immediately stop the process and could cause a loss of work.

    Here is a code sample for killing MS Paint:

    public static void KillPaint()
            {
                System.Diagnostics.Process[] procs = null;
    
                try
                {
                    procs = Process.GetProcessesByName("mspaint");
    
                    Process mspaintProc = procs[0];
    
                    if (!mspaintProc.HasExited)
                    {
                        mspaintProc.Kill();
                    }
                }
                finally
                {
                    if (procs != null)
                    {
                        foreach (Process p in procs)
                        {
                            p.Dispose();
                        }
                    }
                }
            }
    

    HTH


    -Scosby
    Microsoft Community Contributor
    Tuesday, November 15, 2011 9:53 PM
  • i need to clese the process

    this code is closed prosrss and then  opening this

    Wednesday, November 16, 2011 8:30 PM
  • The code shows already how to end a process. So you need to know how to start some other process? The class Process can be used. But maybe you want to give us more details about your requirements, because you can set a lot of different things with Process.

    Process.Start("C:\Somewhere\SomeApp.exe");
    will start SomeApp.exe in c:\Somewhere.

    With kind regards,

    Konrad

    Wednesday, November 16, 2011 9:22 PM
  • He's probably trying to close a service process. (Automatically started process)

    Regards, MusicDemon
    Wednesday, November 16, 2011 10:49 PM