新流程出现故障

本文关键字:故障 新流程 | 更新日期: 2023-09-27 17:58:56

我在这里找不到我想要的答案,但如果是,请链接它,我会关闭这个重复的帖子。

作为我正在进行的程序的一部分,我希望按照以下顺序发生三件简单的事情:

1.)显示字幕进度条2.)通过CMD在中运行一些命令,并将输出移动到可访问的字符串3.)停止/隐藏进度条

我看到的问题是我的代码没有按顺序执行,我非常困惑为什么。它似乎进入了第2-1-3步,这是不可能的。

更奇怪的是,如果我在步骤1和步骤2之间取消对消息框的评论,事情就会按顺序执行。

新的CMD流程有什么不正常的地方吗?

这是我对这种方法的代码:

        //STEP 1 - Updates label and starts progress bar
        lblSelectDiagnostic.Text = "Diagnostic Running";
        progressBarDiag.Visible = true;
        progressBarDiag.MarqueeAnimationSpeed = 100;
        //MessageBox.Show("Status Updated");
        //STEP 2 - Runs "Test Internet Connection"
        //Gets selected diagnostic name
        string strSelectedDiag = listBoxDiagnostics.SelectedItem.ToString();
        var name = strSelectedDiag.Substring(strSelectedDiag.LastIndexOf(':') + 1);
        strSelectedDiag = name.Trim();
        if (strSelectedDiag.Contains("Test Internet Connection"))
        {
            //Pings Google
            ProcessStartInfo info = new ProcessStartInfo();
            info.RedirectStandardError = true;
            info.RedirectStandardInput = true;
            info.RedirectStandardOutput = true;
            info.UseShellExecute = false;
            info.FileName = "cmd.exe";
            info.CreateNoWindow = true;
            //Creates new process
            Process proc = new Process();
            proc.StartInfo = info;
            proc.Start();
            //Writes commands
            using (StreamWriter writer = proc.StandardInput)
            {
                if (writer.BaseStream.CanWrite)
                {
                    writer.WriteLine("ping www.google.com");
                    writer.WriteLine("exit");
                }
                writer.Close();
            }
            string PingGoogle = proc.StandardOutput.ReadToEnd();
            proc.Close();
        }
        //STEP 3 - Resets label and stops progress bar
        progressBarDiag.MarqueeAnimationSpeed = 0;
        progressBarDiag.Visible = false;
        lblSelectDiagnostic.Text = "Select Diagnostic to Run:";

-谢谢!

新流程出现故障

进度条将不会显示,因为您在逻辑所在的同一线程中绘制进度条。您必须在另一个线程中执行此操作。最简单的方法是使用后台工作者

这将帮助您:http://msdn.microsoft.com/en-us/library/cc221403(v=vs.95).aspx