WINFORM窗体多线程异步加载数据,该怎么解决
展开全部
首先在窗体上放一个button和一个picturebox
(picturebox visible为false)这个多线程的目的是点击按钮后图片正常的运行,另一线程在睡眠10秒后在执行。
private void button1_Click(object sender, EventArgs e)
{
//开启线程
System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(this.Result));
pictureBox1.Visible = true;
button1.Enabled = false;
thread.Start();
}
private void Result()
{
bool ok = this.Do();
this.BeginInvoke(new System.Threading.ThreadStart(delegate()
{
if (ok) MessageBox.Show("成功", "提示");
else MessageBox.Show("失败", "提示");
pictureBox1.Visible = false;
button1.Enabled = true;
}));
}
private bool Do()
{
System.Threading.Thread.Sleep(10000);
return true;
}
(picturebox visible为false)这个多线程的目的是点击按钮后图片正常的运行,另一线程在睡眠10秒后在执行。
private void button1_Click(object sender, EventArgs e)
{
//开启线程
System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(this.Result));
pictureBox1.Visible = true;
button1.Enabled = false;
thread.Start();
}
private void Result()
{
bool ok = this.Do();
this.BeginInvoke(new System.Threading.ThreadStart(delegate()
{
if (ok) MessageBox.Show("成功", "提示");
else MessageBox.Show("失败", "提示");
pictureBox1.Visible = false;
button1.Enabled = true;
}));
}
private bool Do()
{
System.Threading.Thread.Sleep(10000);
return true;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询