您的位置:首页 > 编程语言 > C#

C#使用System.Timers.Timer类实现一个Button锁定功能(windows form)

2013-02-04 17:08 435 查看
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;//消除异常:Cross-thread operation not valid:Control 'button1' accessed from a thread other than the thread it was created on .
}
System.Timers.Timer t = new System.Timers.Timer(5000);

private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("This Button has been locked");
t.Start();
t.AutoReset = false;
t.Enabled = true;
button1.Enabled = false;
t.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);

}
private void  timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
t.Stop();
this.button1.Enabled = true;//必须加上Control.CheckForIllegalCrossThreadCalls = false;否则报异常:Cross-thread operation not valid:Control 'button1' accessed from a thread other than the thread it was created on .
}
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show(Owner,"OK");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐