-1

I want to end a process that I started with a ProcessStartInfo, here's the code to start it

public class MyProcess
{
    void OpenWithStartInfo()
    {
        ProcessStartInfo startInfo = new ProcessStartInfo("cmd.exe");
        startInfo.WindowStyle = ProcessWindowStyle.Hidden;
        startInfo.UseShellExecute = true;
        startInfo.FileName = "C:/StartBot.bat";
        Process.Start(startInfo);
    }

    public static void Start()
    {
        MyProcess myProcess = new MyProcess();
        myProcess.OpenWithStartInfo();
    }
    public static void End()
    {
               
    }
}

I want to end the process that I started with my End() function, but I have no idea how to go about doing that. Any help will be greatly appreciated.

Jackdaw
  • 7,626
  • 5
  • 15
  • 33
  • 1) Look at this post, how start a process asynchronously: [Is there any async equivalent of Process.Start?](https://stackoverflow.com/questions/10788982/is-there-any-async-equivalent-of-process-start). 2) And the following post how to stop a process: [How can I stop async Process by CancellationToken?](https://stackoverflow.com/questions/34199628/how-can-i-stop-async-process-by-cancellationtoken) – Jackdaw Oct 25 '20 at 23:04
  • Does this answer your question? [How do I kill a process using Vb.NET or C#?](https://stackoverflow.com/questions/116090/how-do-i-kill-a-process-using-vb-net-or-c) – Peter Duniho Oct 26 '20 at 22:19

1 Answers1

1

Process.Start returns a Process that you can call Kill() on assuming your store the reference somewhere:

    public class MyProcess
    {
        Process process;
        void OpenWithStartInfo()
        {
            ProcessStartInfo startInfo = new ProcessStartInfo("cmd.exe");
            startInfo.WindowStyle = ProcessWindowStyle.Hidden;
            startInfo.UseShellExecute = true;
            startInfo.FileName = "C:/StartBot.bat";
            process = Process.Start(startInfo);
        }

        static MyProcess myProcess = new MyProcess();

        public static void Start()
        {
            myProcess.OpenWithStartInfo();
        }
        public static void End()
        {
            myProcess.process.Kill();
        }
    }
mm8
  • 163,881
  • 10
  • 57
  • 88