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

Post date: Mar 22, 2012 11:20:49 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();