C# 如何在流程完成时加载进度条?

C# 如何在流程完成时加载进度条?,c#,process,progress-bar,C#,Process,Progress Bar,我正在使用以下命令ping主机名: Process p = new Process(); p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.CreateNoWindow = true; p.StartInfo.FileName = "cmd.exe"; p.StartI

我正在使用以下命令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完成后完成加载

谢谢。

甚至有一个关于如何使用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
    }
}
名称空间PingTest
{
使用全局::系统;
使用系统诊断;
使用global::System.Net.NetworkInformation;
使用系统文本;
使用系统线程;
/// 
///ping示例。
/// 
公共类示例
{
#区域静态场
私有静态bool-pingComplete;
#端区
#区域公共方法和运算符
公共静态void DisplayReply(PingReply-reply)
{
if(reply==null)
{
返回;
}
WriteLine(“ping状态:{0}”,reply.status);
if(reply.Status==IPStatus.Success)
{
WriteLine(“地址:{0}”,reply.Address);
WriteLine(“往返时间:{0}”,reply.RoundtripTime);
WriteLine(“生存时间:{0}”,reply.Options.Ttl);
WriteLine(“不分段:{0}”,reply.Options.DontFragment);
WriteLine(“缓冲区大小:{0}”,reply.Buffer.Length);
}
}
公共静态void Main(字符串[]args)
{
如果(args.Length==0)
{
抛出新的ArgumentException(“Ping需要主机或IP地址”);
}
字符串who=args[0];
var WAERER=新自动重置事件(假);
var pingSender=new Ping();
//当引发PingCompleted事件时,
//调用PingCompletedCallback方法。
pingSender.PingCompleted+=PingCompletedCallback;
//创建一个包含32字节数据的缓冲区以进行传输。
const string Data=“aaaaaaaaaaaaaaaaaaaaaaaaaaaa”;
byte[]buffer=Encoding.ASCII.GetBytes(数据);
//等待12秒钟,等待答复。
常数int超时=12000;
//设置变速器的选项:
//数据可以通过64个网关或路由器
//在它被销毁之前,数据包
//不能分割。
var options=新的PingOptions(64,true);
WriteLine(“生存时间:{0}”,options.Ttl);
WriteLine(“不分段:{0}”,options.DontFragment);
//异步发送ping。
//使用服务员作为用户令牌。
//回调完成后,它可以唤醒此线程。
pingComplete=false;
var watch=Stopwatch.StartNew();
watch.Start();
pingSender.SendAsync(谁、超时、缓冲区、选项、服务员);

虽然(!pingComplete&&watch.elapsedmillishes),但真正的问题是,如何确定进度条的位置?您完全可以在进程执行时让进度条运行,并对其进行更新,但除非您要读取和解释STD输出(即使对于“静默”进程也不起作用),你不知道它的值是什么。我可以给你一些通用的线程代码来更新进度条,但就评估“进程完成”而言,我认为你会走运。