如何在c#中给ctrl + c进程?

时间:2013-01-23 09:37:42

标签: c# .net multithreading command-line vmware

我正在尝试创建一个创建进程的后台工作程序,它会生成一些ovf命令。在中间我尝试通过发送ctrl + c中止操作。这个网站上也有类似的问题,但没有一个能解决问题。

private void DeployOVF()
{
    p = new Process();
    ProcessStartInfo pi = new ProcessStartInfo("ovftool.exe", "--machineOutput "+ FileName + " vi://uname:pwd@Ip Address");
    pi.UseShellExecute = false;
    pi.RedirectStandardOutput = true;
    pi.RedirectStandardInput = true;
    pi.RedirectStandardError = true;
    pi.CreateNoWindow = true;
    p.StartInfo = pi;
    p.Start();
    StreamReader myStreamReader = p.StandardOutput;
    string myString;
    bool progressStarted = false;
    while ((myString = myStreamReader.ReadLine()) != null)
    {
        //Logic to display the progress
    }
    p.WaitForExit();
    p.Close();
}

这是我发送ctrl + c以中止进度的地方,

private void button1_Click(object sender, EventArgs e)
{
    p.StandardInput.Write("\x3");
    p.StandardInput.Close();
}

1 个答案:

答案 0 :(得分:1)

如果进程正在响应(即其前台线程正在响应信号),那么您应该使用:

p.CloseMainWindow();

如果没有,您应该能够使用Kill中止该过程(不洁净):

p.Kill();

//Then wait for the process to end
p.WaitForExit();
p.Close();