WPF and Standard Output redirection - Weird Error

S

sundarvenkata

Hi All,

I wanted to develop a WPF application to capture the output of a
command line program and display it in a GUI.

However I get a weird error in the following code:

System.Diagnostics.Process pfadminProc = new
System.Diagnostics.Process();
pfadminProc.StartInfo = new
ProcessStartInfo();
pfadminProc.StartInfo.RedirectStandardInput = true;
pfadminProc.StartInfo.RedirectStandardOutput = true;
pfadminProc.StartInfo.RedirectStandardError = true;
pfadminProc.StartInfo.FileName = "process";
pfadminProc.StartInfo.Arguments = "arguments";
pfadminProc.Start();
StreamReader outputReader =
pfadminProc.StandardOutput;
pfadminProc.WaitForExit();

When I try to execute the last line, I get an error saying that some
"Window1.xaml" resource could not be found. What in the world does
*executing a process* have anything to do with the XAML files. Can
somebody enlighten me??

Thanks,
Sundar
 
W

Willy Denoyette [MVP]

sundarvenkata said:
Hi All,

I wanted to develop a WPF application to capture the output of a
command line program and display it in a GUI.

However I get a weird error in the following code:

System.Diagnostics.Process pfadminProc = new
System.Diagnostics.Process();
pfadminProc.StartInfo = new
ProcessStartInfo();
pfadminProc.StartInfo.RedirectStandardInput = true;
pfadminProc.StartInfo.RedirectStandardOutput = true;
pfadminProc.StartInfo.RedirectStandardError = true;
pfadminProc.StartInfo.FileName = "process";
pfadminProc.StartInfo.Arguments = "arguments";
pfadminProc.Start();
StreamReader outputReader =
pfadminProc.StandardOutput;
pfadminProc.WaitForExit();

When I try to execute the last line, I get an error saying that some
"Window1.xaml" resource could not be found. What in the world does
*executing a process* have anything to do with the XAML files. Can
somebody enlighten me??

No, unless you post a complete code sample. The error you get has nothing to
do with the last line, what we need is what you are doing with outputReader
after the WaitForExit.

Willy.
 
S

sundarvenkata

The WaitForExit () is the last line. When I single step through the
code with Visual Studio I get an Exception with the message as said
above, when executing this statement. I am sure this can be reproduced
for any WPF application.

For instance see the sample app in Visual Studio:

Window1.xaml.cs
*********************
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Diagnostics;


namespace Sample
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>

public partial class Window1 : System.Windows.Window
{

public Window1()
{
InitializeComponent();
//Start the pfadmin process
Process pfadminProc = new Process();
ProcessStartInfo pfadminStartInfo = new
ProcessStartInfo();
pfadminStartInfo.FileName = "process";
pfadminStartInfo.Arguments = "arguments";
pfadminStartInfo.UseShellExecute = true;
pfadminProc.StartInfo = pfadminStartInfo;
pfadminStartInfo.RedirectStandardOutput = true;
pfadminStartInfo.RedirectStandardError = true;
//The following line throws an Exception
pfadminProc.Start();
}

}
}

Window1.xaml
******************
<Window x:Class="Sample.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Sample" Height="300" Width="300"<Grid>

</Grid>
</Window>
 
W

Willy Denoyette [MVP]

sundarvenkata said:
The WaitForExit () is the last line. When I single step through the
code with Visual Studio I get an Exception with the message as said
above, when executing this statement. I am sure this can be reproduced
for any WPF application.

For instance see the sample app in Visual Studio:

Window1.xaml.cs
*********************
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Diagnostics;


namespace Sample
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>

public partial class Window1 : System.Windows.Window
{

public Window1()
{
InitializeComponent();
//Start the pfadmin process
Process pfadminProc = new Process();
ProcessStartInfo pfadminStartInfo = new
ProcessStartInfo();
pfadminStartInfo.FileName = "process";
pfadminStartInfo.Arguments = "arguments";
pfadminStartInfo.UseShellExecute = true;
pfadminProc.StartInfo = pfadminStartInfo;
pfadminStartInfo.RedirectStandardOutput = true;
pfadminStartInfo.RedirectStandardError = true;
//The following line throws an Exception
pfadminProc.Start();
}

}
}

Window1.xaml
******************
<Window x:Class="Sample.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Sample" Height="300" Width="300"
<Grid>

</Grid>
</Window>



You should set UseShellExecute to true,
As a result, your code throws an exception with following message : "The
Process object must have the UseShellExecute property set to false in order
to redirect IO streams" , that means you throw from within your Window
constructor, with the exception you are seeing as a result.
Lesson learned, wrap this code in a try catch block, so at least you know
what's the real cause of the problem, and preferably don't run such code in
the Window constructor, this constructor is meant to set-up your
applications top Windows, starting other activities should be done
elsewhere.


Willy.
 
S

sundarvenkata

I have tried doing UseShellExecute=false also. Still the problem
persists. The code throws this Exception:

{"Cannot create instance of 'Window1' defined in assembly 'TrialMon,
Version=1.0.2866.25665, Culture=neutral, PublicKeyToken=null'.
Exception has been thrown by the target of an invocation. Error in
markup file 'Window1.xaml' Line 1 Position 9."}

Can someone tell me how this is related to starting a process.
 
W

Willy Denoyette [MVP]

sundarvenkata said:
I have tried doing UseShellExecute=false also. Still the problem
persists. The code throws this Exception:

{"Cannot create instance of 'Window1' defined in assembly 'TrialMon,
Version=1.0.2866.25665, Culture=neutral, PublicKeyToken=null'.
Exception has been thrown by the target of an invocation. Error in
markup file 'Window1.xaml' Line 1 Position 9."}

Can someone tell me how this is related to starting a process.



Please do as I asked you, wrap your code in a try/catch block , something
like this will do.

public Window1()
{
InitializeComponent();
//Start the pfadmin process
try
{
....
pfadminProc.Start();
}
catch (Exception ex)
{
this.Title = ex.Message; // set the title to the exception
message
}

Or better, remove this from the constructor an put it in a handler (say a
button handler).

Willy.
 
S

sundarvenkata

I solved the problem by calling the process in a separate thread.
There seems to be no solution for this problem when the application is
single threaded.

Thanks Willy for your help,
Sundar
 
W

Willy Denoyette [MVP]

sundarvenkata said:
I solved the problem by calling the process in a separate thread.
There seems to be no solution for this problem when the application is
single threaded.

Thanks Willy for your help,
Sundar


Your problem is due to the fact that you call it in the Window constructor
and, or your code throws an exception, or takes too long to return to the
constructor.
Anyway, something similar works for me (executing netstat -a), all you
should do is move the code to a handler and wrap it in a try/catch block. If
you have to wait for the process to exit, you can run it from another
thread.
Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top