C#获取EventHandler进程信息

时间:2018-08-01 09:10:33

标签: c# eventhandler

我启动了几个进程,我想知道哪个进程称为OutputHandler,但是我无法获得有关发送进程的任何信息。 当我尝试读取属性时,它总是抛出InvalidOperationException

void ExecString()
{
    using (Process process = new Process())
    {
        process.StartInfo.FileName = executeExe;
        process.StartInfo.Arguments = string.Format("{0}/{1}@{2} @{3}", parameter0, parameter1, parameter2, parameter3);
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.RedirectStandardOutput = true;
        process.StartInfo.RedirectStandardError = true;
        process.StartInfo.CreateNoWindow = true;

        process.EnableRaisingEvents = true;
        process.OutputDataReceived += OutputHandler;

        process.Start();
        process.BeginOutputReadLine();
    }
}   

void OutputHandler(object sendingProcess, DataReceivedEventArgs output)
{
    try
    {
        OutputText = output.Data;
        var tmpProcess = (Process)sendingProcess;
        var testId = tmpProcess.Id; // Throw Exception
    }
    catch (InvalidOperationException e)
    {
        OutputText = e.Message;
    }
}

1 个答案:

答案 0 :(得分:0)

您可以尝试一下吗,它可能会对您有所帮助;

Process currentProcess = Process.GetCurrentProcess(); 
var pid = currentProcess.Id;