async/await可以实现不阻塞主线程的情况下等待子线程的计算结果
private async void FormLoading_Load(object sender, EventArgs e)
{
Control.CheckForIllegalCrossThreadCalls = false;
Task taskSetTxt = new Task(this.SetPBText);
taskSetTxt.Start();
Task<bool> taskMovPic = new Task<bool>(this.MovePic);
taskMovPic.Start();
//等待子线程的返回值,但不阻塞,主线程继续执行
await taskMovPic;
//当进度条加载到100%后返回值为true,这时隐藏加载窗体,显示客户端主窗体
if (taskMovPic.Result)
{
this.Hide();
m_formClient.Show();
}
/**************总结:利用async/await关键字可以在主线程中等待子线程的计算结果,重点在于在主线程等待的过程中不会阻塞***********/
}
public void SetPBText()
{
Graphics g = progressBar1.CreateGraphics();
Font mf = new System.Drawing.Font("宋体", 10);
Brush mb = System.Drawing.Brushes.White;
Point mp = new System.Drawing.Point(progressBar1.Width / 2 - 60, progressBar1.Height / 2 - 8);
while (true)
{
//this.progressBar1.PerformStep();
g.DrawString(string.Format("Loading....{0}%", this.progressBar1.Value), mf, mb, mp);
if (this.progressBar1.Value == 100)
{
//m_bLoadIsOK = true;
//return;
}
//Thread.Sleep(1);
}
}