我启动一个process.exited用于发送程序指令的过程,该过程已完成.
它运行正常,但我需要向方法发送一个参数Process_Exited().像这样的东西:
process.exited += Process_Exited(jobnumber);
Run Code Online (Sandbox Code Playgroud)
但这不起作用.这是我的代码:
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();
}
Run Code Online (Sandbox Code Playgroud)
然后转到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
}
Run Code Online (Sandbox Code Playgroud)
我想process_Exited()从x264_thread 发送一个参数的方法
只需使用lambda:
proc.Exited += (sender, e) => process_Exited(sender, e, JobNumber);
Run Code Online (Sandbox Code Playgroud)
编译器现在生成大量IL,隐藏类和隐藏方法,使其全部工作.
Lambdas非常适合签名和传递其他状态.
您无法更改事件处理程序的签名,Process.Exited但在事件处理程序代码中,您可以强制转换sender为类型Process,因为sender它实际上是原始流程的实例。现在您已经了解了流程,您可以确定您的 JobNumber:
public void process_Exited(object sender, EventArgs e)
{
Process myProcess = (Process)sender;
// use myProcess to determine which job number....
// DoWork
}
Run Code Online (Sandbox Code Playgroud)
现在,要获取哪个作业编号与哪个进程关联,您可以执行以下操作:
在你的类中放置一个字段:
private Dictionary<Process, string> _processDictionary =
new Dictionary<Process,string>();
Run Code Online (Sandbox Code Playgroud)
在您的x264_thread方法中,您可以在创建流程后添加一个条目:
_processDictionary.Add(proc, jobnumber);
Run Code Online (Sandbox Code Playgroud)
然后在您的事件处理程序中您可以检索该值:
public void process_Exited(object sender, EventArgs e)
{
Process myProcess = (Process)sender;
// use myProcess to determine which job number....
var jobNumber = _processDictionary[myProcess];
_processDictionary.Remove(myProcess);
// DoWork
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3170 次 |
| 最近记录: |