Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#/.NET: Closing another process outside the main window

Tags:

c#

.net

process

I just wanna ask your opinion/suggestion on how to 'terminate' a running application/process is C#

Right now, I do have the following codes:

    Process myProcess;
    private void btnOpen_Click(object sender, RoutedEventArgs e)
    {
        DirectoryInfo di = new DirectoryInfo(System.Environment.GetFolderPath(Environment.SpecialFolder.Programs));
        myProcess = Process.Start(di + @"\Wosk\Wosk.appref-ms"); // opening a file coming for Startup Menu
    }

    private void btnClose_Click(object sender, RoutedEventArgs e)
    {
        myProcess.Kill(); // not working - Cannot process request because the process has exited
    }

I also tried myProcess.Close(); but nothing's happening.

like image 764
abramlimpin Avatar asked Dec 30 '25 19:12

abramlimpin


1 Answers

You should have a look at

Process.HasExited Property

A process can terminate independently of your code. If you started the process using this component, the system updates the value of HasExited automatically, even if the associated process exits independently.

like image 169
Adriaan Stander Avatar answered Jan 01 '26 08:01

Adriaan Stander



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!