Application.Idle 事件
描述:当应用程序完成处理并即将进入空闲状态时发生。如果您有必须执行的任务在线程变为空闲之前,请将它们附加到此事件。
1 public partial class Form1 : Form 2 { 3 /// <summary> 4 /// 空闲期间执行的次数 5 /// </summary> 6 private int executeTimes = 0; 7 8 public Form1() 9 { 10 InitializeComponent(); 11 } 12 13 private void btnGetTime_Click(object sender, EventArgs e) 14 { 15 Application.Idle += Application_Idle; 16 } 17 18 /// <summary> 19 /// 当程序完成处理并进入空闲状态时发生 20 /// </summary> 21 /// <param name="sender"></param> 22 /// <param name="e"></param> 23 private void Application_Idle(object sender, EventArgs e) 24 { 25 txtTime.Text = DateTime.Now.ToString(); 26 27 executeTimes++; 28 if (executeTimes==9) 29 { 30 Application.Idle -= Application_Idle; 31 MessageBox.Show("已经在CPU空闲时间执行10次"); 32 } 33 } 34 }