我怎么能有一个进度条加载完成的过程

本文关键字:过程 加载 怎么能 有一个 | 更新日期: 2023-09-27 17:50:48

我使用以下命令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命令期间有一个进度条"加载",并在ping完成时完成加载?

谢谢。

我怎么能有一个进度条加载完成的过程

http://msdn.microsoft.com/en-us/library/system.net.networkinformation.pingcompletedeventhandler(v=vs.110).aspx甚至有一个如何使用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
    }
}