James0886

Hi all this is what my app does, The app opens a batch file, and runs it in Hidden mode and a new windows appears and it will redirect the text from the batch file to the new windows, My Problem is that the app runs and it copys some of the text from the batch file to the app window then, the window frezzes (it only shows few text lines) not all the lines from the batch file, this is my first time doing this, I need some 1 to tell Me the right way to do it, and what im doing wrong

This is the code im using:


Code Snippet

public void proWorld()
{
log showLog = new log();
showLog.Show();
Process cmd = new Process();
string path = "import.bat";
bool exit = true;
cmd.StartInfo.UseShellExecute = false;
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.CreateNoWindow = true;
cmd.StartInfo.FileName = "import.bat";
cmd.Start();
//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
while (exit == true)
{
string output = cmd.StandardOutput.
showLog.textBox1.Text = output;
showLog.textBox1.Focus();
//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
if (cmd.HasExited)
{

MessageBox.Show("Import Complete");
exit = false;
if (File.Exists(path))
{
File.Delete(path);
}
}

}

}



thx alot in advance.


Re: Visual C# General OutPut Redirect Problems

Steve Py

When you redirect the standard output, you have to read the contents from the command periodically as it's updated.

I "think" the only change you need to make is:

string output = cmd.StandardOutput.ReadToEnd();

But I'm not sure if that will clear the StdOut cache between reads or not. (Whether each read brings back the complete text from the start, or whether each additional read only brings back what changes.) If it doesn't clear the cache, your above code should work with the change. However, if it's flickering then only displaying the last few lines then try:

StringBuilder outputText = new StringBuilder();

while (exit)
{
outputText.Append(cmd.StandardOutput.ReadToEnd();
showLog.textBox1.Text = outputText;
// ...


That will append each read to a string builder that will serve as a cache for all output reads that you can drive your textbox from. (It's more memory efficient than appending strings to the existing textbox text.)






Re: Visual C# General OutPut Redirect Problems

Citizen on the earth

Hi James0886,

As I understand from your description, you would like to get the standard output from a batch file in the command line.

You can try to change your sample codes as the following and check if it works for you.

StreamReader outputReader = null;

try
{

//Create Process Start information
ProcessStartInfo processStartInfo =
new ProcessStartInfo(txtExecutable.Text.Trim(), txtParameter.Text.Trim());
processStartInfo.ErrorDialog = false;
processStartInfo.UseShellExecute = false;
processStartInfo.RedirectStandardError = true;
processStartInfo.RedirectStandardInput = true;
processStartInfo.RedirectStandardOutput = true;

//Execute the process
Process process = new Process();
process.StartInfo = processStartInfo;
bool processStarted = process.Start();
if (processStarted)
{
//Get the output stream
outputReader = process.StandardOutput;
process.WaitForExit();

//Display the result
displayText += outputReader.ReadToEnd();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
if(outputReader != null)
{
outputReader.Close();
}
}
}
else
{
MessageBox.Show("Please select executable.");
}

Besides, try to check out this article about "How to redirect Standard Input/Output of an application" for details- http://www.codeproject.com/csharp/ProcessStartDemo.asp

Hope this helps,

Citizens on the earth





Re: Visual C# General OutPut Redirect Problems

James0886

thx alot guys for the help...i got a new problem, the batch file i execute takes alot of resource, how can i lower the resources it uses





Re: Visual C# General OutPut Redirect Problems

Steve Py

AFAIK there's not a lot of control you can take on an external process as to how much processing power it requisitions other than setting the process' priority.

When you set up your process instance, use:
myProcess.PriorityClass = ProcessPriorityClass.BelowNormal

or if it's really something that doesn't need to be run unless there's nothing else going on:
myProcess.PriorityClass = ProcessPriorityClass.Idle


That won't impact how much memory or disk access the process needs to use, but it should allow other processes (including your main application) to be more responsive.