-3

I make an application that runs ping and displays the result in a text field. When I click the start ping button, the GUI hangs and nothing is output to the text field. Why this GUI hangs is understandable, the GUI is waiting for the console application to finish. I do not understand how then to implement the output in the text field from the console application.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;

namespace WindowsFormsApp4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Class1 ping = new Class1();
            ping.startPing();
            string output = ping.output();
            richTextBox1.AppendText(output + "\n");
            richTextBox1.Update();
        }

        static private void richTextBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }

    class Class1
    {
        private Process p = new Process();

        public void startPing()
        {
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.CreateNoWindow = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.FileName = "c:/windows/system32/ping";
            p.StartInfo.Arguments = "8.8.8.8 -t";
            p.Start();
        }

        public string output()
        {
            string output = p.StandardOutput.ReadToEnd();
            p.WaitForExit();
            return output;
        }
    }
}
Michael
  • 21
  • 5
  • The GUI freezes because you're running your ping on the same thread as the GUI, thus blocking it from updating until your operation has finished, you might want to look into starting up a new thread or background worker and running the ping on it – MindSwipe Mar 22 '19 at 09:28
  • 3
    What do you think `p.WaitForExit();` does? Does the documentation suggest an alternate approach? https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process.waitforexit?view=netframework-4.7.2 – mjwills Mar 22 '19 at 09:28
  • There are also a lot of examples on pulling the results from the console into c# – BugFinder Mar 22 '19 at 09:28
  • Possible duplicate of [Why is my process's Exited method not being called?](https://stackoverflow.com/questions/4504170/why-is-my-processs-exited-method-not-being-called) – mjwills Mar 22 '19 at 09:29
  • Try using `p.Exited` event (do not forget to allow it - `p.EnableRaisingEvents = true;`) and drop `p.WaitForExit();` – Dmitry Bychenko Mar 22 '19 at 09:45

1 Answers1

-4

This code will usefull to resolve your query.

  private void button1_Click(object sender, EventArgs e)
  {
          var worker = new BackgroundWorker();
          worker.DoWork += (o, ea) =>
          {
                Class1 ping = new Class1();
                ping.startPing();
                string output = ping.output();
                richTextBox1.AppendText(output + "\n");
                richTextBox1.Update();

          };
          worker.RunWorkerCompleted += (o, ea) =>
          {
                //You will get pointer when this worker finished the job.
          };
          worker.RunWorkerAsync();
    }

Let me know if any issue after implement it with your source.

ShSakariya
  • 34
  • 1
  • 5
  • Just posting code doesn't help the OP understand the solution. Please provide an explanation along with the code. – T_Bacon Mar 22 '19 at 09:38