我启动了一个进程,该进程使用 process.exited
向程序发送有关如何处理已完成进程的指令。
它工作正常,但我需要向 Process_Exited()
方法发送一个参数。像这样:
process.exited += Process_Exited(jobnumber);
但这行不通。这是我的代码:
public void x264_thread(string jobnumber, string x264_argument, string audio_argument)
{
file = new System.IO.StreamWriter("c:\\output\\current.out");
mysqlconnect("UPDATE h264_encoder set status = 'running' where jobnumber = '" + jobnumber + "'");
var proc = new Process();
proc.StartInfo.FileName = "C:\\ffmbc\\ffmbc.exe";
proc.StartInfo.Arguments = x264_argument;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.EnableRaisingEvents = true;
proc.StartInfo.CreateNoWindow = true;
proc.ErrorDataReceived += proc_DataReceived;
proc.OutputDataReceived += proc_DataReceived;
proc.Exited += process_Exited(JobNumber); //This is where I would like to include a argument
proc.Start();
proc.BeginErrorReadLine();
proc.BeginOutputReadLine();
}
然后进入process_Exited
方法:
public void process_Exited(object sender, EventArgs e, string JobNumber) //This does not work. It does not pass the string JobNumber
{
//Do Stuff
}
我想向 process_Exited()
方法发送一个来自 x264_thread 的参数
最佳答案
只需使用 lambda:
proc.Exited += (sender, e) => process_Exited(sender, e, JobNumber);
编译器现在生成大量 IL、一个隐藏类和一个隐藏方法以使其全部正常工作。
Lambda 非常适合调整签名和传递附加状态。
关于C# 通过 Process.Exited 发送参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17621608/