2014-03-18 79 views
0

我使用下面來ping主機名:如何通過完成一個過程來加載進度條?

 Process p = new Process(); 
     p.StartInfo.UseShellExecute = false; 
     p.StartInfo.RedirectStandardOutput = true; 
     p.StartInfo.CreateNoWindow = true; 
     p.StartInfo.FileName = "cmd.exe"; 
     p.StartInfo.Arguments = pingData; 
     p.Start(); 

     p.WaitForExit(); 
     string result = p.StandardOutput.ReadToEnd(); 

     System.Windows.Forms.Clipboard.SetText(result); 
     if(p.HasExited) 
     { 
      richTextBox1.Text = result; 
      outPut = result; 
      MessageBox.Show("Ping request has completed. \n Results have been copied to the clipboard.");     
     } 

反正是有ping命令時,我可以有一個進度條「負荷」,並完成裝載時,平安已完成?

謝謝。

+3

http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ping – SLaks

+1

真正的問題是,你怎麼確定哪些將進度條放在?你肯定可以讓一個進度條在進程執行的同時運行並更新它,但除非你要讀取並解釋STD OUT(甚至對於「無聲」進程不起作用),否則你不會知道什麼值是。我可以給你一些通用的線程代碼來更新進度條,但就評估「流程完成」而言,我認爲你會走運。 – BradleyDotNET

回答

-1

甚至有一個如何使用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 
    } 
} 
相關問題