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"); }
相关文章推荐
- 写一个system.data.entity的simpledatarepo,实现crudq这些功能,不需要引入entityframework,直接可以使用,用到objectset
- C#使用FileSystemWatcher控件实现的文件监控功能示例
- C#使用FileSystemWatcher控件实现的文件监控功能示例
- C# 使用Panel控件实现在一个窗体中嵌套另一个窗体
- C# 使用WebRequest 实现FTP常用功能
- 完成一个学生管理程序,使用学号作为键添加5个学生对象,并可以将全部信息保存在文件中,可以实现对学生信息的学号查找,输出全部学生信息的功能。
- 使用redis实现一个购物车功能
- 在C#中实现打印功能(C#中PrintDialog,PrintDocument的使用) (转)
- 在C#中实现打印功能(C#中PrintDialog,PrintDocument的使用)
- 使用UIScrollView和UIPageControl实现一个简单的图片浏览功能
- C#使用 System.Net.Mail发送邮件功能
- ios-day20-01(使用UIWebView实现一个简单功能的浏览器)
- C#使用泛型数组简单实现一个列表List
- 在C#中实现打印功能(C#中PrintDialog,PrintDocument的使用)
- C#.NET vs2010中使用IrisSkin4.dll轻松实现WinForm窗体换肤功能
- cocos2d中如何使用图片纹理图集的加载来实现一个动画的功能
- 在C#中实现打印功能(C#中PrintDialog,PrintDocument的使用)
- C#和Qt实现的对于异常处理的一个使用策略
- 完成一个学生管理程序,使用学号作为键添加5个学生对象,并可以将全部信息保存在文件中,可以实现对学生信息的学号查找,输出全部学生信息的功能。
- Android 使用MVP模式 实现一个查询功能。