Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Send argument through Process.Exited

Tags:

c#

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

like image 569
user2475310 Avatar asked Jul 12 '13 18:07

user2475310


People also ask

What C is used for?

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 ...

What is the full name of C?

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.

Is C language easy?

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.

What is C language?

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.


2 Answers

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.

like image 60
usr Avatar answered Sep 19 '22 11:09

usr


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
}
like image 23
Brad Rem Avatar answered Sep 21 '22 11:09

Brad Rem