发送CTRL_C / SIGINT到一个进程从C# [英] Send CTRL_C/SIGINT to a process from C#

查看:256
本文介绍了发送CTRL_C / SIGINT到一个进程从C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想中断通过cmd.exe运行的命令。在下面的代码中,我使用ping www.stackoverflow.com -t作为示例。

  public void Run b $ b {
System.Diagnostics.ProcessStartInfo si = new System.Diagnostics.ProcessStartInfo(cmd.exe);

si.RedirectStandardInput = true;
si.RedirectStandardOutput = true;
si.RedirectStandardError = true;
si.UseShellExecute = false;
si.CreateNoWindow = false;
//si.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;

System.Diagnostics.Process console = new Process();
console.StartInfo = si;

console.EnableRaisingEvents = true;
console.OutputDataReceived + = proc_OutputDataReceived;
console.ErrorDataReceived + = proc_ErrorDataReceived;

console.Start();

console.BeginOutputReadLine();
console.BeginErrorReadLine();

console.StandardInput.WriteLine(ping www.stackoverflow.com -t);

Thread.Sleep(4000);
bool success = GenerateConsoleCtrlEvent((uint)0,(uint)console.SessionId);
if(!success)
{
MessageBox.Show(Error Code:+ Marshal.GetLastWin32Error()。ToString());
}

char asdf =(char)0x3;

console.StandardInput.WriteLine('\x3');
console.StandardInput.WriteLine(asdf);
console.StandardInput.Write(asdf);

//console.StandardInput.Close();

console.StandardInput.WriteLine(@exit);
console.WaitForExit();
}

GenerateConsoleCtrlEvent的错误代码为6.



我遵循了以下指令:



  1. http://pastebin.com/vQuWQD8F





解决方案



/ div>

这可能是一个非常常见的问题,发送键以及一些其他消息,到应用程序。记住,如果您发送的应用程序具有比您的程序更高的权限评估,它通常不会成功发送密钥或消息。尝试在管理权限下运行程序。

p>

看到这是不是你的问题你想看看使用 ServiceController 类记录此处。它允许比 Process 更好的控制,强烈建议在这些情况下的可靠性。



这里有两个教程让你开始:



http://www.codeproject.com/Articles/31688/Using-the-ServiceController-in-C-to-stop-and-start
http://www.c-sharpcorner.com/uploadfile/mahesh/servicecontroller-in-C-Sharp/



希望这有助于你!


I would like to interrupt a command running through cmd.exe. In the code below, I am using ping www.stackoverflow.com -t as an example.

    public void Run()
    {
        System.Diagnostics.ProcessStartInfo si = new System.Diagnostics.ProcessStartInfo("cmd.exe");

        si.RedirectStandardInput = true;
        si.RedirectStandardOutput = true;
        si.RedirectStandardError = true;
        si.UseShellExecute = false;
        si.CreateNoWindow = false;
        //si.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;

        System.Diagnostics.Process console = new Process();
        console.StartInfo = si;

        console.EnableRaisingEvents = true;
        console.OutputDataReceived += proc_OutputDataReceived;
        console.ErrorDataReceived += proc_ErrorDataReceived;

        console.Start();

        console.BeginOutputReadLine();
        console.BeginErrorReadLine();

        console.StandardInput.WriteLine("ping www.stackoverflow.com -t");

        Thread.Sleep(4000);
        bool success = GenerateConsoleCtrlEvent((uint)0, (uint)console.SessionId);
        if (!success)
        {
            MessageBox.Show("Error Code: " + Marshal.GetLastWin32Error().ToString());
        }

        char asdf = (char)0x3;

        console.StandardInput.WriteLine('\x3');
        console.StandardInput.WriteLine(asdf);
        console.StandardInput.Write(asdf);

        //console.StandardInput.Close();

        console.StandardInput.WriteLine(@"exit");
        console.WaitForExit();
    }

The error code from GenerateConsoleCtrlEvent is 6.

I have followed the instructions from:

  1. Can I send a ctrl-C (SIGINT) to an application on Windows?

  2. http://pastebin.com/vQuWQD8F

  3. How to send keys instead of characters to a process?

However, I am unable to interrupt the process.

Any help greatly appreciated,

解决方案

This can be a very common problem with sending keys as well as some other messages, to an application. Remember, that if the application you are sending to has higher permissions evaluation than your program, it usually will not succeed in sending the keys or message. Try running your program under administrative privileges. Or, if you are debugging through Visual Studio, run Visual Studio under administrative privileges.

EDIT:

seeing as how this is not your problem you want to take a look at using the ServiceController class documented here. It allows much finer control than Process does and is highly recommended for reliability in these situations. I will leave my previous answer simply because its a common problem that someone else may find helpful

Here are two tutorials to get you started:

http://www.codeproject.com/Articles/31688/Using-the-ServiceController-in-C-to-stop-and-start http://www.c-sharpcorner.com/uploadfile/mahesh/servicecontroller-in-C-Sharp/

Hope this helps you!

这篇关于发送CTRL_C / SIGINT到一个进程从C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆