Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to turn this code to "async mode", because my process frezze when I start a long process standardOutput redirection of DOS like programs.

C#
public void Process_Ex(string fileName, string arguments)
        {
            My problem is here

            var psi = new ProcessStartInfo(fileName, arguments);

            // Set the options.
            psi.UseShellExecute = false;
            psi.ErrorDialog = false;
            psi.CreateNoWindow = true;

            // Specify CodePage Output
            var encoding = Encoding.GetEncoding(850);
            psi.StandardOutputEncoding = encoding;
            psi.StandardErrorEncoding = encoding;

            // Specify redirection.
            psi.RedirectStandardError = true;
            psi.RedirectStandardInput = true;
            psi.RedirectStandardOutput = true;

            // Create process.
            var process = new Process();
            process.EnableRaisingEvents = true;
            process.StartInfo = psi;

            process.Start();
            
            string s = process.StandardOutput.ReadToEnd();
            richTextBox1.Text = s;
            toolStripStatusLabel1.Text = "Runngig Process : " + fileName;
            
            return;
        }


What I have tried:

I'm sory if code like randomly writed.
C#
process.BeginOutputReadLine();
process.WaitForExit();
process.BeginErrorReadLine();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
string otp = process_ErrorDataReceived;
richTextBox1.Text = process_ErrorDataReceived;
StreamReader reader = new StreamReader();
var sortOutput = new StringBuilder("");
process.OutputDataReceived += new
DataReceivedEventHandler(SortOutputHandler);
process.BeginOutputReadLine();
process.OutputDataReceived += p_OutputDataReceived;
private void SortOutputHandler(object sendingProcess, DataReceivedEventArgs
outLine)
{
var sortOutput = new StringBuilder("");

sortOutput.Append(Environment.NewLine + "[" + numOutputLines.ToString() + "]
- " + outLine.Data);
sortOutput.Append(Environment.NewLine + "[" + "] - " + outLine.Data);
richTextBox1.AppendText(sortOutput.ToString());
if (this.InvokeRequired)
this.Invoke(new UpdateLabelDelegate(this.));
richTextBox1.Invoke();
void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
string RTB = (e.Data);
richTextBox1.Invoke((Delegate) => richTextBox1.Text = RTB;
richTextBox1.Text += RTB;
Console.Write(e.Data);
Posted
Updated 20-Apr-19 17:01pm

Use a background worker. Update the text box (i.e. UI) from the progress reporting event and / or the completed event which run on the UI thread. Use a concurrent queue and / or pass state objects to the event handlers.

BackgroundWorker Class (System.ComponentModel) | Microsoft Docs[^]
 
Share this answer
 
Comments
GSylvain55 20-Apr-19 0:28am    
Yes, I understand. But can you write a symple code to give me a direction. I've been trying for 3 days.
thank you very much !
I find a other method, it work.
But, I would have liked to know how to do with BackgroundWorker.

C#
public void Process_Ex(string fileName, string arguments)
{

    var psi = new ProcessStartInfo(fileName, arguments);

    // Set the options.
    psi.UseShellExecute = false;
    psi.ErrorDialog = false;
    psi.CreateNoWindow = true;

    // Specify CodePage Output
    var encoding = Encoding.GetEncoding(850);
    psi.StandardOutputEncoding = encoding;
    psi.StandardErrorEncoding = encoding;

    // Specify redirection.
    psi.RedirectStandardError = true;
    psi.RedirectStandardInput = true;
    psi.RedirectStandardOutput = true;

    // babkground worker
    //Process.

    // Create process.
    var process = new Process();
    process.EnableRaisingEvents = true;

    process.StartInfo = psi;

    process.Start();
    process.OutputDataReceived += new
    DataReceivedEventHandler(SortOutputHandler);
    process.BeginOutputReadLine();
    process.BeginErrorReadLine();

    return;
}

void SortOutputHandler(object sender, DataReceivedEventArgs e)
{
    Trace.WriteLine(e.Data);
    this.BeginInvoke(new MethodInvoker(() =>
    {
        richTextBox1.AppendText(Environment.NewLine + e.Data ?? string.Empty);
    }));
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900