1,978
社区成员
发帖
与我相关
我的任务
分享
private void button2_Click(object sender, EventArgs e)
{
textBox2.Text = string.Empty;
ThreadStart start = new ThreadStart(Ping);
Thread t = new Thread(start);
t.Start();
}
private void Ping()
{
AsynPingDelegate caller = new AsynPingDelegate(this.ExecPingProcess);
AsyncCallback asyncPingCallback = new AsyncCallback(PingCallBack);
IAsyncResult result = caller.BeginInvoke(out output, PingCallBack, null);
result.AsyncWaitHandle.WaitOne();
caller.EndInvoke(out output, result);
}
private void ExecPingProcess(out string s)
{
Process p = new Process();
p.StartInfo.FileName = "ping";
p.StartInfo.UseShellExecute = false;
p.StartInfo.Arguments = textBox1.Text;
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
StreamReader sr = p.StandardOutput;
s = sr.ReadToEnd();
p.WaitForExit();
p.Close();
}
private void PingCallBack(IAsyncResult result)
{
// 此处的目的是:确保UI线程间的安全。
MethodDelegate methodDelegate = new MethodDelegate(UpdateOutput);
this.Invoke(methodDelegate);
}