简体   繁体   中英

c# unable to kill process

Basically I am trying to make a program that will be able to launch and close a .bat file. I have managed to get it to launch the bat file, however I am unable to figure out how to get it to close the .bat file.

Here is the class code:

using System;
using System.Diagnostics;
using System.Threading;
using System.ComponentModel;

namespace Launcher
{
    class MyProcess
    {
        public static void LaunchProcess()
        {
            Process myProcess = new Process();
            try
            {
                myProcess.StartInfo.UseShellExecute = false;
                myProcess.StartInfo.FileName = "C:\example.bat";
                myProcess.StartInfo.CreateNoWindow = true;
                myProcess.Start();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }
}

I am aware that to kill the process I would have to use myProcess.Kill(); however I am unsure how to add it to the code, as if I was to re-call the 'launchprocess' then the myProcess would be overwritten

Your code uses a local variable to store the process information and doesn't save it for later operations. If you want to keep this as a static method then return the Process as a result:

public static class MyProcess
{
    public static void LaunchProcess()
    {
        Process myProcess = new Process();
        try
        {
            myProcess.StartInfo.UseShellExecute = false;
            myProcess.StartInfo.FileName = "C:\example.bat";
            myProcess.StartInfo.CreateNoWindow = true;
            myProcess.Start();
            return myProcess;
        }
        catch (Exception e)
        {
            myProcess.Dispose();
            Console.WriteLine(e.Message);
        }
    }
}

Now you have a Process object that represents the new process and can call the Kill method on it if you need to get rid of it.

The alternative is to change this to non-static and have the class instance terminate the process when requested. Depending on what you're doing it might make sense to implement IDisposable :

public class MyProcess : IDisposable
{
    private Process myProcess = null;

    ~MyProcess()
    {
        Dispose(false);
    }

    public void Dispose(bool dispose)
    {
        Dispose();
    }

    public void Dispose()
    {
        if (myProcess != null)
        {
            if (!myProcess.HasExited)
                myProcess.Kill();
            myProcess.Dispose();
            myProcess = null;
        }
    }

    public bool LaunchProcess()
    {
        try
        {
            myProcess = new Process();
            myProcess.StartInfo.UseShellExecute = false;
            myProcess.StartInfo.FileName = "C:\example.bat";
            myProcess.StartInfo.CreateNoWindow = true;
            myProcess.Start();
            return true;
        }
        catch (Exception e)
        {
            myProcess.Dispose();
            Console.WriteLine(e.Message);
        }
        return false;
    }
}

That should ensure that the process doesn't outlive your own program.

如何将"Exit"命令添加到批处理文件

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