I know Powershell has background job functionality with Start-Job, Wait-Job, etc., but is it possible to use the Process class from System.Diagnostics in .Net to achieve the same thing? If so, what would be the best way to do it/what advantages would it have over the default Powershell mechanism of running background jobs?
I know Powershell has background job functionality with Start-Job , Wait-Job , etc., but
Share
You can certainly use the Process object to “start” the executable asychronously and with the process object you get back you can test to see if the EXE is done or kill the process. The trick is getting the output and error stream information back without interfering with the console while the program is runnng so you can do other stuff. From the MSDN docs, it looks like using the
BeginOutputReadLinemight do the trick:Although if you want the background behavior you would need to perform the StandardOutput.ReadToEnd() on a background thread and then create a mechanism to retrieve that output from the main console thread which seems like a lot of work and at the moment I can think of any advantages over PowerShell background jobs.
Another approach is to create a runspace to do the bg job work as this blog post by Jim Truher points out.