home
products
contribute
download
documentation
forum
Home
Forums
New posts
Search forums
What's new
New posts
All posts
Latest activity
Members
Registered members
Current visitors
Donate
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Search titles only
By:
Menu
Log in
Register
Navigation
Install the app
Install
More options
Contact us
Close Menu
Forums
MediaPortal 1
MediaPortal 1 Plugins
Executing an external application....and getting its output
Contact us
RSS
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="samuel337" data-source="post: 52400" data-attributes="member: 10347"><p>That article doesn't take advantage of the new .NET 2.0 features in System.Diagnostics.Process. I've just completed a project involving this sort of stuff, so here's some code:</p><p></p><p>[code]</p><p>System.Diagnostics.Process newProcess;</p><p></p><p>private void StartSomething()</p><p>{</p><p> //build processstartinfo object</p><p> System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo();</p><p> psi.RedirectStandardOutput = true;</p><p> psi.UseShellExecute = false;</p><p> psi.EnableRaisingEvents = true;</p><p></p><p> psi.FileName = <replace with filename here>;</p><p></p><p> newProcess = new System.Diagnostics.Process();</p><p> newProcess.StartInfo = psi;</p><p></p><p> newProcess.Start();</p><p></p><p> newProcess.OutputDataReceived += new System.Diagnostics.DataReceivedEventHandler(newProcessOutputHandler);</p><p> newProcess.BeginOutputReadLine();</p><p>}</p><p></p><p> private void newProcessOutputHandler(object sender, System.Diagnostics.DataReceivedEventArgs e)</p><p> {</p><p> //do stuff here. Use e.Data to get the outputted line</p><p> }</p><p></p><p>//to determine if something has exited, use newProcess.HasExited, or listen in on the Exited event of the newProcess object</p><p>[/code]</p><p></p><p>Now I assume that you want to do something useful with the outputted lines, something like display them in a text box. Note that the eventhandler runs in a different thread to the UI thread, hence in order to do something to the UI, you need to use delegates/callbacks. Here's some more code (from MSDN) to do that:</p><p></p><p>[code]</p><p> delegate void WriteConsoleOutputCallback(string output);</p><p></p><p> private void WriteToConsoleOutputBox(string text)</p><p> {</p><p> // InvokeRequired required compares the thread ID of the</p><p> // calling thread to the thread ID of the creating thread.</p><p> // If these threads are different, it returns true.</p><p> if (this.txtConsoleOutput.InvokeRequired)</p><p> {</p><p> WriteConsoleOutputCallback callBack = new WriteConsoleOutputCallback(WriteToConsoleOutputBox);</p><p> this.Invoke(callBack, new object[] { text });</p><p> }</p><p> else</p><p> {</p><p> txtConsoleOutput.Text += Environment.NewLine + text;</p><p> txtConsoleOutput.SelectionStart = txtConsoleOutput.Text.Length;</p><p> txtConsoleOutput.ScrollToCaret();</p><p> }</p><p> }</p><p>[/code]</p><p></p><p>This code assumes you have a textbox called txtConsoleOutput and it has the multiline property enabled.</p><p></p><p>To use the above code, just change the event handler in the initial code to:</p><p>[code]</p><p> private void newProcessOutputHandler(object sender, System.Diagnostics.DataReceivedEventArgs e)</p><p> {</p><p> WriteToConsoleOutputBox(e.Data);</p><p> }</p><p>[/code]</p><p></p><p>HTH</p><p></p><p>Sam</p></blockquote><p></p>
[QUOTE="samuel337, post: 52400, member: 10347"] That article doesn't take advantage of the new .NET 2.0 features in System.Diagnostics.Process. I've just completed a project involving this sort of stuff, so here's some code: [code] System.Diagnostics.Process newProcess; private void StartSomething() { //build processstartinfo object System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(); psi.RedirectStandardOutput = true; psi.UseShellExecute = false; psi.EnableRaisingEvents = true; psi.FileName = <replace with filename here>; newProcess = new System.Diagnostics.Process(); newProcess.StartInfo = psi; newProcess.Start(); newProcess.OutputDataReceived += new System.Diagnostics.DataReceivedEventHandler(newProcessOutputHandler); newProcess.BeginOutputReadLine(); } private void newProcessOutputHandler(object sender, System.Diagnostics.DataReceivedEventArgs e) { //do stuff here. Use e.Data to get the outputted line } //to determine if something has exited, use newProcess.HasExited, or listen in on the Exited event of the newProcess object [/code] Now I assume that you want to do something useful with the outputted lines, something like display them in a text box. Note that the eventhandler runs in a different thread to the UI thread, hence in order to do something to the UI, you need to use delegates/callbacks. Here's some more code (from MSDN) to do that: [code] delegate void WriteConsoleOutputCallback(string output); private void WriteToConsoleOutputBox(string text) { // InvokeRequired required compares the thread ID of the // calling thread to the thread ID of the creating thread. // If these threads are different, it returns true. if (this.txtConsoleOutput.InvokeRequired) { WriteConsoleOutputCallback callBack = new WriteConsoleOutputCallback(WriteToConsoleOutputBox); this.Invoke(callBack, new object[] { text }); } else { txtConsoleOutput.Text += Environment.NewLine + text; txtConsoleOutput.SelectionStart = txtConsoleOutput.Text.Length; txtConsoleOutput.ScrollToCaret(); } } [/code] This code assumes you have a textbox called txtConsoleOutput and it has the multiline property enabled. To use the above code, just change the event handler in the initial code to: [code] private void newProcessOutputHandler(object sender, System.Diagnostics.DataReceivedEventArgs e) { WriteToConsoleOutputBox(e.Data); } [/code] HTH Sam [/QUOTE]
Insert quotes…
Verification
Post reply
Forums
MediaPortal 1
MediaPortal 1 Plugins
Executing an external application....and getting its output
Contact us
RSS
Top
Bottom