I start a process that uses process.exited
to send the program instructions on what to do with the process is finished.
It works fine, But I need to send a argument to the Process_Exited()
method. Something like this:
process.exited += Process_Exited(jobnumber);
But that does not work. Here is my code:
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();
}
Then it goes to the process_Exited
Method:
public void process_Exited(object sender, EventArgs e, string JobNumber) //This does not work. It does not pass the string JobNumber
{
//Do Stuff
}
I would like to send the process_Exited()
method an argument from the x264_thread
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.
Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.
C is an imperative procedural language supporting structured programming, lexical variable scope, and recursion, with a static type system. It was designed to be compiled to provide low-level access to memory and language constructs that map efficiently to machine instructions, all with minimal runtime support.
Just use a lambda:
proc.Exited += (sender, e) => process_Exited(sender, e, JobNumber);
The compiler now generates lots of IL, a hidden class and a hidden method to make it all work.
Lambdas are great for adapting signatures and passing additional state.
You can not change the signature of an event handler like Process.Exited
but in your event handler code, you can cast sender
as type Process
since sender
is really the instance of your original process. Now that you know the process, you could determine your JobNumber:
public void process_Exited(object sender, EventArgs e)
{
Process myProcess = (Process)sender;
// use myProcess to determine which job number....
// DoWork
}
Now, to get which job number is associated with which process, you could do this:
Put a field in your class:
private Dictionary<Process, string> _processDictionary =
new Dictionary<Process,string>();
In your x264_thread
method you could add an entry after you create the process:
_processDictionary.Add(proc, jobnumber);
Then in your event handler you could retrieve the value:
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With