c# - 如何给c#中的进程赋予ctrl + c?

标签 c# .net multithreading command-line vmware

我正在尝试创建一个后台 worker 来创建一个进程,该进程会发出一些 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();
}

最佳答案

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

p.CloseMainWindow();

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

p.Kill();

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

关于c# - 如何给c#中的进程赋予ctrl + c?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14476461/

相关文章:

.net - 如何检查我的对象是否已正确处置?

c# - .NET,为什么我必须使用 *Specified 属性来强制序列化?有没有办法不这样做?

android 持续轮询...如何?

c# - 我的类(class)在序列化过程中丢失了方法

c# - Docker:将文件写入物理或共享路径,使用docker和netcore3.1

c# - 如何让一个进程在 c#/.net 中的另一个进程中触发事件?

java - 线程何时空闲?

c++ - 在 C++ 中使用多线程时是否可以读取写了一半、损坏的原始变量?

c# - 为什么这个方法调用不明确?

c# - 如何在多个 c# 调用中使用临时表