循环更改标签
本文关键字:标签 循环 | 更新日期: 2023-09-27 18:36:17
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 个点)
如果必须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");
}
}
}
你可以执行以下操作:
Action.Text = "Installing.";
Process process = Process.Start(root + Installfolder + "dotnetFX45.exe", "/q");
while(!process.WaitForExit(1000)){
Action.Text += ".";
}
但是,如果在 Windows 窗体应用程序的事件处理程序中执行此操作,则此操作将无法按预期工作。您将阻止 UI 线程,因此它永远没有机会重新绘制和显示更新的文本。在这种情况下,您应该使用 BackgroundWorker
,并在DoWork
中显示以下内容:
Process process = Process.Start(root + Installfolder + "dotnetFX45.exe", "/q");
while(!process.WaitForExit(1000)){
worker.ReportProgress(0.5);
}
然后通过在ProgressChanged
中添加 .
s 来更新 Text
属性。
使用 RxNet,示例代码
var interval = Observable.Interval(TimeSpan.FromMilliseconds(1000));
interval.Subscribe(
() => Action.Text += ".",
() => Action.Text = "Completed");