c#无法杀死进程

时间:2016-03-26 05:15:49

标签: c# batch-file

基本上我正在尝试创建一个能够启动和关闭.bat文件的程序。我设法让它启动bat文件,但我无法弄清楚如何让它关闭.bat文件。

这是类代码:

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);
            }
        }
    }
}

我知道要杀死这个过程我必须使用myProcess.Kill();但是我不确定如何将它添加到代码中,就好像我要重新调用“启动过程”一样。然后myProcess将被覆盖

2 个答案:

答案 0 :(得分:1)

您的代码使用本地变量来存储流程信息,而不会将其保存以供以后的操作使用。如果您想将此作为静态方法保留,则返回Process作为结果:

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);
        }
    }
}

现在您有一个代表新进程的Process对象,如果您需要删除它,可以在其上调用Kill方法。

另一种方法是将其更改为非静态,并让类实例在请求时终止进程。根据您正在做的事情,实施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;
    }
}

这应该确保该过程不会超过您自己的程序。

答案 1 :(得分:0)

如何将import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; public class MainActivity extends Activity { private WebView mWebView = (WebView) findViewById(R.id.activity_main_webview); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(amapps.com.uhss.R.layout.activity_main); WebView mWebView = (WebView) findViewById(R.id.activity_main_webview); mWebView.setWebViewClient(new WebViewClient()); WebSettings webSettings = mWebView.getSettings(); webSettings.setJavaScriptEnabled(false); mWebView.loadUrl("http://uhsswordandshield.com/"); mWebView.getSettings().setSupportMultipleWindows(true); } 命令添加到批处理文件

相关问题