甚至有一个如何使用Ping的例子。根据您希望显示进度条的方式,您可以修改示例以使事件处理程序设置一个标志,指示已完成ping。然后在主执行线程中添加一个while循环,以占用进度条的时间/超时值的百分比填充进度条。下面是重新格式化作为例子,例如
namespace PingTest
{
using global::System;
using System.Diagnostics;
using global::System.Net.NetworkInformation;
using System.Text;
using System.Threading;
/// <summary>
/// The ping example.
/// </summary>
public class PingExample
{
#region Static Fields
private static bool pingComplete;
#endregion
#region Public Methods and Operators
public static void DisplayReply(PingReply reply)
{
if (reply == null)
{
return;
}
Console.WriteLine("ping status: {0}", reply.Status);
if (reply.Status == IPStatus.Success)
{
Console.WriteLine("Address: {0}", reply.Address);
Console.WriteLine("RoundTrip time: {0}", reply.RoundtripTime);
Console.WriteLine("Time to live: {0}", reply.Options.Ttl);
Console.WriteLine("Don't fragment: {0}", reply.Options.DontFragment);
Console.WriteLine("Buffer size: {0}", reply.Buffer.Length);
}
}
public static void Main(string[] args)
{
if (args.Length == 0)
{
throw new ArgumentException("Ping needs a host or IP Address.");
}
string who = args[0];
var waiter = new AutoResetEvent(false);
var pingSender = new Ping();
// When the PingCompleted event is raised,
// the PingCompletedCallback method is called.
pingSender.PingCompleted += PingCompletedCallback;
// Create a buffer of 32 bytes of data to be transmitted.
const string Data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] buffer = Encoding.ASCII.GetBytes(Data);
// Wait 12 seconds for a reply.
const int Timeout = 12000;
// Set options for transmission:
// The data can go through 64 gateways or routers
// before it is destroyed, and the data packet
// cannot be fragmented.
var options = new PingOptions(64, true);
Console.WriteLine("Time to live: {0}", options.Ttl);
Console.WriteLine("Don't fragment: {0}", options.DontFragment);
// Send the ping asynchronously.
// Use the waiter as the user token.
// When the callback completes, it can wake up this thread.
pingComplete = false;
var watch = Stopwatch.StartNew();
watch.Start();
pingSender.SendAsync(who, Timeout, buffer, options, waiter);
while (!pingComplete && watch.ElapsedMilliseconds <= Timeout)
{
// Do display stuff for your progress bar here.
}
// Prevent this example application from ending.
// A real application should do something useful
// when possible.
waiter.WaitOne();
Console.WriteLine("Ping example completed.");
Console.ReadLine();
}
#endregion
#region Methods
private static void PingCompletedCallback(object sender, PingCompletedEventArgs e)
{
// If the operation was canceled, display a message to the user.
if (e.Cancelled)
{
Console.WriteLine("Ping canceled.");
// Let the main thread resume.
// UserToken is the AutoResetEvent object that the main thread
// is waiting for.
((AutoResetEvent)e.UserState).Set();
}
// If an error occurred, display the exception to the user.
if (e.Error != null)
{
Console.WriteLine("Ping failed:");
Console.WriteLine(e.Error.ToString());
// Let the main thread resume.
((AutoResetEvent)e.UserState).Set();
}
PingReply reply = e.Reply;
pingComplete = true;
DisplayReply(reply);
// Let the main thread resume.
((AutoResetEvent)e.UserState).Set();
}
#endregion
}
}
http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ping – SLaks
真正的问题是,你怎么确定哪些将进度条放在?你肯定可以让一个进度条在进程执行的同时运行并更新它,但除非你要读取并解释STD OUT(甚至对于“无声”进程不起作用),否则你不会知道什么值是。我可以给你一些通用的线程代码来更新进度条,但就评估“流程完成”而言,我认为你会走运。 – BradleyDotNET