Here’s a small trick I found useful. I recently wrote a tool that performs an ‘xcopy’ backup of the source files in my visual studio projects directory. The tool is a command line application that spawns a few other command line applications (xcopy for one) and redirects their output to its own standard output. Since some spawned processes are long running, I want to show their output as they run, I created a static method that does it all using the Task class from the .Net 4 framework.
Code Snippet
- static int SpawnAndRedirect(string command, string args)
- {
- Process process;
- ProcessStartInfo processStartInfo;
- processStartInfo = new ProcessStartInfo(command, args);
- processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
- processStartInfo.RedirectStandardOutput = true;
- processStartInfo.UseShellExecute = false;
- process = Process.Start(processStartInfo);
- /* Start a background task that copies the spawned process
- * standard output stream to our own standard output stream */
- Task.Factory.StartNew(() =>
- {
- process.StandardOutput.BaseStream.CopyTo(Console.OpenStandardOutput());
- });
- process.WaitForExit();
- return process.ExitCode;
- }
Just added this blog to my favorites. I enjoy reading your blogs and hope you keep them coming!
ReplyDeleteAll in all I see Horrible Bosses, you could do a lot worst, if you’re one of the people who the films’ content appeals to see it, it would be a safe choice.
ReplyDelete