0

I'm currently trying to create new NodeJS process and while it's running, put it's console output into my winform textbox.

Whenever this process is executed, it's freezing main thread as if form is waiting for this process to exit. After the process is closed thats when the console output is added to the textbox.

What I'm trying to achieve is simultaneously have this node process running in the background and have whatever it's outputing in the textbox.

Edit 1:

I Managed to run the console without freezing main thread but the output only shows when the process is closed

My current code:

    private void Btn_connect_Click(object sender, EventArgs e)
        {
            if(backgroundWorker1.IsBusy != true)
            {
                backgroundWorker1.RunWorkerAsync();
            }
        }

        private void BackgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            var worker = sender as BackgroundWorker;
            nodeProcess = new Process();
            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.FileName = "node.exe";
            startInfo.Arguments = @"path" + " arg1 arg2 arg3";
            startInfo.UseShellExecute = false;
            startInfo.WindowStyle = ProcessWindowStyle.Hidden;
            startInfo.RedirectStandardOutput = true;
            nodeProcess.StartInfo = startInfo;

            nodeProcess.Start();

            while (worker.CancellationPending != true)
            {
                Thread.Sleep(200);
                AddText(nodeProcess.StandardOutput.ReadToEnd());
                worker.ReportProgress(1);
            }

            e.Cancel = true;
        }

        public void AddText(string text)
        {
            if(txt_log.InvokeRequired)
            {
                txt_log.Invoke(new Action<string>(AddText), new object[] { text });
                return;
            }

            txt_log.Text += "\n " + text;
        }

1 Answer 1

1

Instead of a BackgroundWorker you could try using Process.BeginOutputReadLine, Process.OutputDataReceived, and Process.Exited.

void StartProcess()
{
    Process nodeProcess = new Process();
    ProcessStartInfo startInfo = new ProcessStartInfo();
    startInfo.FileName = "node.exe";
    startInfo.Arguments = @"path" + " arg1 arg2 arg3";
    startInfo.UseShellExecute = false;
    startInfo.WindowStyle = ProcessWindowStyle.Hidden;
    startInfo.RedirectStandardOutput = true;
    nodeProcess.StartInfo = startInfo;

    nodeProcess.EnableRaisingEvents = true;
    nodeProcess.Exited += nodeProcess_Exited;
    nodeProcess.OutputDataReceived += nodeProcess_OutputDataReceived;

    nodeProcess.Start();
    nodeProcess.BeginOutputReadLine();
}

void nodeProcess_Exited(object sender, EventArgs e)
{
    // Do something when the process exits, if you need to.
    // You'll want to check InvokeRequired before you modify any of your form's controls.
}

void nodeProcess_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
    if (txt_log.InvokeRequired)
    {
        txt_log.Invoke(new Action<string>(AddText), new object[] { e.Data });
        return;
    }
    txt_log.Text += "\n " + e.Data;
}
1
  • Thank you very much!, works like a charm, must have eye-balled the docs.
    – nJoy
    Oct 12, 2019 at 0:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.