标签在循环中更改

时间:2016-02-25 08:16:08

标签: c# visual-studio loops

 public partial class Balloon : Form
    {
        public Balloon()
        {
            InitializeComponent();
            TopMost = true;
            Rectangle r = Screen.PrimaryScreen.WorkingArea;
            this.StartPosition = FormStartPosition.Manual;
            this.Location = new Point(Screen.PrimaryScreen.WorkingArea.Width - this.Width - 1, Screen.PrimaryScreen.WorkingArea.Height - this.Height - 8);


            InstallStart2();
        }

        private async void InstallStart2()
        {

            if (Dot35 == "Yes")
            {
                this.Titel.Text = "The Knowhow Installer is installing:";
                this.Action.Text = "Microsoft .Net Framework 2 & 3";
                if (File.Exists(root + Installfolder + "dotnetfx35.exe"))
                {
                    Process process = Process.Start(root + Installfolder + "dotnetfx35.exe", "/q /norestart");

                    while (!process.HasExited) ;

                    bool installFinished1 = false;
                    int k = -1;
                    string[] dots = new string[] { "Microsoft .Net Framework 2 & 3.", "Microsoft .Net Framework 2 & 3..", "Microsoft .Net Framework 2 & 3..." };

                    while (!installFinished1)
                    {
                        Action.Text = dots[++k % 3];
                        await Task.Delay(TimeSpan.FromSeconds(1.0));
                    }

                    process.WaitForExit();
                }
                else
                {
                    this.TopMost = false;
                    int num = (int)MessageBox.Show("dotnetfx35.exe not found", "Error");
                }
            }
            await Task.Delay(TimeSpan.FromSeconds(2.0));







            if (Dot45 == "Yes")
            {
                this.Titel.Text = "The Knowhow Installer is installing:";
                this.Action.Text = "Microsoft .Net Framework 4.5";
                if (File.Exists(root + Installfolder + "dotnetFx45.exe"))
                {
                    Process process = Process.Start(root + Installfolder + "dotnetFX45.exe", "/q");

                    bool installFinished = false;
                    int k = -1;
                    string[] dots = new string[] { "Microsoft .Net Framework 4.5.", "Microsoft .Net Framework 4.5..", "Microsoft .Net Framework 4.5..." };

                    while (!installFinished)
                    {
                        Action.Text = dots[++k % 3];
                        await Task.Delay(TimeSpan.FromSeconds(1.0));
                    }





                    while (!process.HasExited) ;

                }
                else
                {
                    this.TopMost = false;
                    int num = (int)MessageBox.Show("dotnetFx45.exe not found", "Error");
                }
            }

            await Task.Delay(TimeSpan.FromSeconds(2.0));


            this.Action.Text = "Done";

            new SoundPlayer(root + otherfolder + "Done.wav").Play();
            await Task.Delay(TimeSpan.FromSeconds(5.0));

            Environment.CurrentDirectory = dir;
            this.Close();
            new InstallDone().Show();




        }

    }

这是代码的一部分,它适用于第一次安装,但我找不到放置bool installFinished1 = true;的位置,以使其转到下一个。现在它只停留在Microsoft .Net Framework 4.5 ...(它在循环中从1点变为3点)

3 个答案:

答案 0 :(得分:1)

您可以执行以下操作:

Action.Text = "Installing.";
Process process = Process.Start(root + Installfolder + "dotnetFX45.exe", "/q");
while(!process.WaitForExit(1000)){
   Action.Text += ".";
}

但是,如果您在Windows窗体应用程序中的事件处理程序中执行此操作,则无法按预期工作。您将阻止UI线程,因此它永远不会有机会重新绘制并显示更新的文本。在这种情况下,您应该在DoWork

中使用BackgroundWorker
Process process = Process.Start(root + Installfolder + "dotnetFX45.exe", "/q");
while(!process.WaitForExit(1000)){
   worker.ReportProgress(0.5);
}

然后在Text中添加.来更新ProgressChanged媒体资源。

答案 1 :(得分:1)

使用RxNet,示例代码

var interval = Observable.Interval(TimeSpan.FromMilliseconds(1000));
interval.Subscribe(
    () => Action.Text += ".", 
    () => Action.Text = "Completed");

答案 2 :(得分:1)

如果必须是await,那么这是最简单的方法:

bool installFinished = false;
int k = -1;
string[] dots = new string[] { "Installing.", "Installing..", "Installing..." };

while (!installFinished)
{
    Action.Text = dots[++k % 3];
    await Task.Delay(TimeSpan.FromSeconds(1.0));
}
Action.Text = "Finished";

完成安装后,请设置:

installFinished = true;

上方循环将停止,Action.Text设置为Finished

其他方式,您可以使用计时器。

修改

根据您更新的代码:

using System.Threading;

bool installFinished = false;

void Install_net45()
{
    Process process = Process.Start(root + Installfolder + "dotnetFX45.exe", "/q");

    while (!process.WaitForExit(1000))
    {
    }
    installFinished = true;
    // Clean up
}

private async void InstallStart2()
{
    if (Dot45 == "Yes")
    {
        this.Titel.Text = "The Knowhow Installer is installing:";
        this.Action.Text = "Microsoft .Net Framework 4.5";
        if (File.Exists(root + Installfolder + "dotnetFx45.exe"))
        {

            installFinished = false;

            Thread t = new Thread(Install_net45);
            t.Start();

            int k = -1;
            string[] dots = new string[] { "Installing.", "Installing..", "Installing..." };

            while (!installFinished)
            {
                Action.Text = dots[++k % 3];
                await Task.Delay(TimeSpan.FromSeconds(1.0));
            }
            Action.Text = "Finished";
            MessageBox.Show("Test");
        }
        else
        {
            this.TopMost = false;
            int num = (int)MessageBox.Show("dotnetFx45.exe not found", "Error");
        }
    }
}