views:

83

answers:

2

Hi folks,

I wish to pass some data to the delegate method, of a Process object, when it fires the Exited event --- i'm not sure how.


I've got some code (in a windows service) that is going to take a while .. so i'm forking off a new process to do it .. like ...

string recipientEmail = "[email protected]";

var commandProcess = new Process
{
    StartInfo = 
        {
            FileName = commandLine,
            Arguments = commandArgs
        }
};
commandProcess.Start();

Now, when this finishes, I wish to do some other stuff. For example, send an email.

Now, that's not too hard when we can :-

commandProcess.EnableRaisingEvents = true;

// Method to handle when the process has exited.
commandProcess.Exited += CommandProcess_Exited;

Now, i'm not sure how i pass the variable recipientEmail to the method CommandProcess_Exited when the Exited event is fired.

eg method which the CommandProcess_Exited method will call :-

private static void SendEmailToRecipient(string recipientEmail)
{
    ....
}

Is this possible?

+4  A: 

You can either make the variable non-local, that is, declare it in the class so it's visible to your event handling method.

Or you can use a delegate and write the method inline, which allows you to access local variables:

commandProcess.Exited += delegate
    {
         SendEmailToRecipient(recipientEmail);
    };
Joey
Hi Johannes - can u please elaborate on the second method. I thought of the first method .. but this is a windows service .. and this class is actually static, not instance .... (which i failed to mention) .. oops :P
Pure.Krome
@ Johannes - I've updated the initial post with the example Email method i want to call.
Pure.Krome
I deleted my answer as for this particular scenario it seemed a little overkill, this is a much easier solution.
James
James, don't delete it. It's a perfectly valid way to do it and others may stumble over this question and look for a better way. This all depends on the exact scenario :-)
Joey
@ Johannes -- re: James + delete .. yep! that's right.. it all helps us learn :)
Pure.Krome
Never thought about it that way...Undeleted :)
James
+1  A: 

For something like this what I tend to do is extend the EventArgs class and create my own version which includes the required data:

public class MyProgramEventArgs : EventArgs
{
     public MyProgramEventArgs()
     {
     }

     public string RecipientEmail { get; set; }
}

Then what you can do is capture the Exited from the program itself, do some pre-processing on the args, then invoke the method:

public static void Main(string[] args)
{
    commandProcess.EnableRaisingEvents = true;
    ...
    commandProcess.Exited += OnProgramExited;
}

public void OnProgramExited(object sender, EventArgs e)
{
    MyProgramEventArgs args = new MyProgramEventArgs();
    args.RecipientEmail = "[email protected]";
    CommandProcess_Exited(sender, args);
}

public void CommandProcess_Exited(object sender, MyProgramEventArgs e)
{
    SendEmailToRecipient(e.RecipientEmail);   
}
James
+1 to you, then :-).
Joey