Just Code‎ > ‎

C# - Using Lambda instead of a separate event handler to handle events

posted Mar 22, 2012, 4:20 AM by Peter Henell   [ updated Mar 22, 2012, 4:33 AM ]
Snippet of how to use a lambda to handle an event.

In this example we will handle the OutputDataReveived event on the Process class to write the output from the started process to our own console window.
ProcessStartInfo p = new ProcessStartInfo(System.Configuration.ConfigurationManager.AppSettings["svnPath"], parameters);
p.WorkingDirectory = path;
p.RedirectStandardOutput = true;
p.UseShellExecute = false;

Process svn = new Process();
svn.StartInfo = p;
svn.OutputDataReceived += (sender, e) => 
    Console.WriteLine(e.Data); // handle the event, datatypes of parameters are infered

svn.Start();
svn.BeginOutputReadLine();

svn.WaitForExit();
Comments