111,094
社区成员




void btn检索_Click(object sender, EventArgs e)
{
启动Timer......;
ThreadPool.QueueUserWorkItem(h =>
{
执行耗时的检索操作......;
this.BeginInvoke((Action)delegate
{
在控件上显示检索结果提示信息。
});
});
}
当 btn检索_Click 已经执行完毕之后(UI 线程回到了底层去刷新界面),才开始耗时的检索操作。因此这是异步的操作。
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Timer timer;
int i;
private void button_Click(object sender, EventArgs e)
{
i = 0;
this.textBox.Text = "停止驾驶";
this.button.Enabled = false;
timer = new Timer();
timer.Interval = 50;
timer.Tick += timer_Tick;
timer.Start();
}
void timer_Tick(object sender, EventArgs e)
{
switch(i)
{
case 20:
this.textBox.Text = "正在上车";
break;
case 40:
this.textBox.Text = "正在关门";
break;
case 80:
this.textBox.Text = "正在行驶";
break;
case 100:
this.textBox.Text = "我下车了!";
this.button.Enabled = true;
timer.Stop();
timer.Dispose();
break;
}
this.progressBar.Value = i;
i++;
}
}
最好设置下button的Enabled属性,防止多次点击
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Timer timer;
int i;
private void button_Click(object sender, EventArgs e)
{
i = 0;
this.textBox.Text = "zh";
timer = new Timer();
timer.Interval = 50;
timer.Tick += timer_Tick;
timer.Start();
}
void timer_Tick(object sender, EventArgs e)
{
switch(i)
{
case 20:
this.textBox.Text = "正在上车";
break;
case 40:
this.textBox.Text = "正在关门";
break;
case 80:
this.textBox.Text = "正在行驶";
break;
case 100:
this.textBox.Text = "我下车了!";
timer.Stop();
timer.Dispose();
break;
}
this.progressBar.Value = i;
i++;
}
}
winform,三个控件 button textbox progressbar,自己去玩吧
public void UpdateStatus()
{
label1.Text = str;
}
int i = 1;
public delegate void TimerFunc();
private void button2_Click(object sender, EventArgs e)
{
timer1.Start();
}
string str = "加载中。。。";
private void timer1_Tick(object sender, EventArgs e)
{
if (i == 10)
{
str = "加载中。。。";
i = 1;
}
str += "。";
TimerFunc func = new TimerFunc(UpdateStatus);
this.Invoke(func, new Object[] { });
i++;
}