我希望每30%进度栏,文字更改。
我想要做的是,当进度条达到25%时,文本会发生变化并停止一秒,然后又会回到50,文本会再次变化,直到达到100%。
这是我的代码:
public WelcomeScreen()
{
InitializeComponent();
_timer.Interval = 2000;
label1.ForeColor = Color.White;
}
private void WelcomeScreen_Load(object sender, EventArgs e)
{
var worker = new BackgroundWorker();
worker.WorkerReportsProgress = true;
worker.DoWork += worker_DoWork;
worker.RunWorkerCompleted += worker_RunWorkerCompleted;
worker.ProgressChanged += worker_ProgressChanged;
worker.RunWorkerAsync();
}
void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
var percentComplete = e.ProgressPercentage;
var userState = (string)e.UserState;
progressBar1.Minimum = 0;
progressBar1.Maximum = 100;
progressBar1.Value = 100;
progressBar1.Step = 25;
if (progressBar1.Step <= 25)
{
label1.Text = "Preparing Setup";
}
else if (progressBar1.Step <= 50)
{
label1.Text = "Preparing Application";
}
else if (progressBar1.Step <= 75)
{
label1.Text = "Preparing Database";
}
else if (progressBar1.Step <= 100)
{
label1.Text = "Preparing Contents";
}
else
{
label1.Text = "Launch Application";
}
}
void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
_timer.Enabled = true;
_timer.Tick += new EventHandler(Timer_Tick);
}
void worker_DoWork(object sender, DoWorkEventArgs e)
{
var currentWorker = (BackgroundWorker)sender;
currentWorker.ReportProgress(0, "Preparing Setup");
currentWorker.ReportProgress(25, "Preparing Application");
currentWorker.ReportProgress(50, "Preparing Database");
currentWorker.ReportProgress(75, "Preparing Contents");
currentWorker.ReportProgress(100, "Launch Application");
}
void Timer_Tick(object sender, EventArgs e)
{
_timer.Enabled = false;
this.Hide();
_login.ShowDialog();
this.Close();
}
当进度条达到100%时,我命令应用程序等到2秒后再显示另一个表单,然后文本变为“启动应用程序”。
答案 0 :(得分:1)
WelcomeScreen_Load
中的所有代码都会阻止UI线程,直到完成为止。这意味着无论你对进度条做了什么,它都不会在它完成之前显示它的变化(它会“跳转”到最后的设置)。
您需要查看Background Workers
。这些允许您执行代码异步,并且每隔一段时间报告当前状态(即:完成百分比)。
所以,这是一个非常简短的例子:
private void WelcomeScreen_Load(object sender, EventArgs e)
{
var worker = new BackgroundWorker();
worker.WorkerReportsProgress = true;
worker.DoWork += worker_DoWork;
worker.RunWorkerCompleted += worker_RunWorkerCompleted;
worker.ProgressChanged += worker_ProgressChanged;
worker.RunWorkerAsync();
}
void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
var percentComplete = e.ProgressPercentage;
var userState = (string)e.UserState;
//do something with these values, like moving your progress bar
progressBar1.Minimum = 0;
progressBar1.Maximum = 100;
progressBar1.Value = percentComplete;
}
void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
// do something when the worker completes, like start your timer
}
void worker_DoWork(object sender, DoWorkEventArgs e)
{
// do the "work" for the background worker
var currentWorker = (BackgroundWorker)sender;
currentWorker.ReportProgress(0, "Just Starting");
// do your first task
currentWorker.ReportProgress(25, "Finish First Task");
// ...
}