最近在DoNet项目中需要用到Application.Idle功能函数,平时基本没用过。今天写点代码测试一下,一目了然。
Application.Idle的英文描述为:“Occurs when the application finished processing and is about to enter idle state.”,字面意思 当应用程序处于空闲状态时执行相应代码。
还是上代码,源代码也可直接下载。
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- namespace 测试Application.Idel
- {
- public partial class Form1 : Form
- {
- private int excuteTimes = 0;//空闲期间执行的次数
- public Form1()
- {
- InitializeComponent();
- }
- /// <summary>
- /// 时间处理,获取最新的系统时间在文本框中显示
- /// </summary>
- private void ProcessTime(object sender, EventArgs e)
- {
- textBox1.Text = System.DateTime.Now.ToString();
- excuteTimes++;
- if (excuteTimes == 9)
- {
- Application.Idle -= new EventHandler(ProcessTime);
- MessageBox.Show("已经在CPU空闲期间执行了10次!");
- }
- }
- /// <summary>
- /// 按扭,获取时间
- /// </summary>
- private void btnGetTime_Click(object sender, EventArgs e)
- {
- Application.Idle += new EventHandler(ProcessTime);
- }
- }
- }
转载于:https://blog.51cto.com/jamesking/1029557