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;
}
}
}