我正在尝试创建一个后台 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/