c#,两个form,第一个form为欢迎界面,有一个进度条,进度条能实时的反应form2的加载情况 20
展开全部
form2:
private int _progress = 0;
/// <summary>
/// 开始异步计算
/// </summary>
public void StartCalc()
{
// 模拟
Thread th = new Thread(new ThreadStart(StartWork));
th.Start();
}
/// <summary>
/// 获取任务进度(百分比)
/// </summary>
/// <returns></returns>
public int GetProgress()
{
return _progress * 100 / 60;
}
/// <summary>
/// 1分钟后线程工作结束
/// </summary>
private void StartWork()
{
for (int i = 0; i < 60;i++ )
{
Thread.Sleep(1000);
_progress = i + 1;
}
}
form1:
private void button1_Click(object sender, EventArgs e)
{
progressBar1.Maximum = 100;
progressBar1.Value = 0;
Form2 frm = new Form2();
frm.StartCalc();
Timer timer = new Timer();
timer.Tick += new EventHandler((obj, ea) =>
{
progressBar1.Value = frm.GetProgress();
if (progressBar1.Value == 100)
{
timer.Stop();
frm.ShowDialog();
}
});
}
private int _progress = 0;
/// <summary>
/// 开始异步计算
/// </summary>
public void StartCalc()
{
// 模拟
Thread th = new Thread(new ThreadStart(StartWork));
th.Start();
}
/// <summary>
/// 获取任务进度(百分比)
/// </summary>
/// <returns></returns>
public int GetProgress()
{
return _progress * 100 / 60;
}
/// <summary>
/// 1分钟后线程工作结束
/// </summary>
private void StartWork()
{
for (int i = 0; i < 60;i++ )
{
Thread.Sleep(1000);
_progress = i + 1;
}
}
form1:
private void button1_Click(object sender, EventArgs e)
{
progressBar1.Maximum = 100;
progressBar1.Value = 0;
Form2 frm = new Form2();
frm.StartCalc();
Timer timer = new Timer();
timer.Tick += new EventHandler((obj, ea) =>
{
progressBar1.Value = frm.GetProgress();
if (progressBar1.Value == 100)
{
timer.Stop();
frm.ShowDialog();
}
});
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询