Application.Idle()方法表示:当应用程序处于空闲状态时执行相应代码。

示例程序

1、界面设计:一个简单的Lable控件

2、代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO; namespace ApplicationIdleDemo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} public System.Timers.Timer timer;
private void Form1_Load(object sender, EventArgs e)
{
InitTimer();
InitRefresh();
Refresh();
} /// <summary>
/// 初始化Timer控件
/// </summary>
private void InitTimer()
{
timer = new System.Timers.Timer();
//到达定时时间的时候执行的事件
timer.Elapsed += new System.Timers.ElapsedEventHandler(TimeUp);
//设置是执行一次(false) 还是一直执行(true)
timer.AutoReset = true;
//是否执行System.Timers.Timer.Elapsed事件
timer.Enabled = true;
//启动
timer.Start(); } /// <summary>
/// 定时到点执行的事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void TimeUp(object sender, System.Timers.ElapsedEventArgs e)
{
Refresh();
} private void Refresh()
{
this.lbl_idle.Text = "进入空闲期";
string strPath = Application.StartupPath + @"test.txt";
using (StreamWriter sw = new StreamWriter(strPath, true))
{
sw.WriteLine("开始进入空闲期,当前时间:" + DateTime.Now);
sw.Close();
}
} private void InitRefresh()
{
//设定IDLE自动结束
Application.Idle += new EventHandler(OnApplicationIdle);
//设定消息过滤
FormMessageFilter MessageFilter = new FormMessageFilter();
MessageFilter.ApplicationActive += new EventHandler(OnApplicationActive);
Application.AddMessageFilter(MessageFilter); } /// <summary>
/// 程序进入空闲时期时会一直执行此事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnApplicationIdle(object sender, EventArgs e)
{
if (timer != null)
timer.Start();
} /// <summary>
/// 当键盘及鼠标事件,关闭timer
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnApplicationActive(object sender, EventArgs e)
{
if (timer != null)
{
timer.Stop();
EndIdle();
} } private void EndIdle()
{
this.lbl_idle.Text = "结束空闲期,进入活动期";
string strPath = Application.StartupPath + @"test.txt";
using (StreamWriter sw = new StreamWriter(strPath,true))
{
sw.WriteLine("开始进入活动期,当前时间:" + DateTime.Now);
sw.Close();
} } } public class FormMessageFilter : IMessageFilter
{
public event EventHandler ApplicationActive; /// <summary>
/// 只要是按键盘及鼠标便会引发事件
/// 因为是为了监视键盘及鼠标,所以均return false;
/// return ture:会把输入的值清除
/// 0x100 /* WM_KEYDOWN
/// 0x101 /* WM_KEYUP
/// 0x200 /* WM_MOUSEMOVE
/// 0x201 /* WM_LBUTTONDOWN
/// 0x202 /* WM_LBUTTONUP
/// 0x203 /* WM_LBUTTONDBLCLK
/// 0x204 /* WM_RBUTTONDOWN
/// 0x205 /* WM_RBUTTONUP
/// 0x206 /* WM_RBUTTONDBLCLK
/// 0x20a /* WM_MOUSEWHEEL
/// </summary>
public bool PreFilterMessage(ref Message m)
{
if (m.Msg == 0x100 || m.Msg == 0x101 || (m.Msg > 0x199 && m.Msg < 0x207) || m.Msg == 0x20a)
{
if (ApplicationActive != null)
{
ApplicationActive(this, new EventArgs());
}
}
return false;
}
}
}

Application.idle方法的更多相关文章

  1. 使用Application.GetResourceStream方法加载资源时得到的总是null

    我们可以预先把程序中用到的资源,如图片,音乐等放入项目中,打包进XAP文档,需要的时候从中调用.下面就说说具体实现方法. 第一步,把数据存进项目. 1.右键点击项目名称-添加-新建文件夹(英文版请自行 ...

  2. WinForm中Application.Idle事件用法

    Application.Idle 事件 描述:当应用程序完成处理并即将进入空闲状态时发生.如果您有必须执行的任务在线程变为空闲之前,请将它们附加到此事件. public partial class F ...

  3. Unity iOS打开AppStore评星页面,浅谈Application.OpenURL()方法。

    http://fairwoodgame.com/blog/?p=38 Unity iOS打开AppStore评星页面,浅谈Application.OpenURL()方法. Posted in  Uni ...

  4. android结束进程、退出application的方法

    1.finish()方法 finish是Activity的类,仅仅针对Activity,当调用finish()时,只是将活动推向后台,并没有立即释放内存,活动的资源并没有被清理:调用finish()方 ...

  5. Android中Application全局方法(变量)的调用

    Application和Actovotu,Service一样是android框架的一个系统组件,当android程序启动时系统会创建一个 application对象,用来存储系统的一些信息.通常我们是 ...

  6. struts2 action中获取request session application的方法

    共四种方式: 其中前两种得到的是Map<String,Object>  后两种得到的才是真正的request对象 而Map就是把request对象中的属性取出做成了键值对而已. [方法一] ...

  7. sql sever 2012重装数据库时,出现cannot find one or more components, Please reinstall the application.解决方法

    错误原因: 由于我将SQL数据库做了删除,重装.在删除的过程中,不小心删除了某个SQL的插件,导致了这种问题的出现. 当我们去操作工具时,也会提示以上错误. 解决办法: 1)去控制面板--所有控制面板 ...

  8. Application.Exit()结束程序,但线程还在的解决方法。

    出现此情况大多原因是使用了多线程编程,或者你所调用的dll使用了多线程.我们知道,一般情况下的线程执行完指定的任务之后是会关闭了的,但是如果对于一些循环类线程,或者忘记关掉的线程时,这个时候就需要我们 ...

  9. Application.Exit()结束程序,但线程还在的解决方法

    转自:http://bbs.51cto.com/thread-970057-1.html 出现此情况大多原因是使用了多线程编程,或者你所调用的dll使用了多线程.  我们知道,一般情况下的线程执行完指 ...

随机推荐

  1. Hadoop On Demand

    原文地址:http://hadoop.apache.org/docs/r1.0.4/cn/hod.html 简介 文档 简介 Hadoop On Demand(HOD)是一个能在大型物理集群上供应虚拟 ...

  2. 【jQuery】将form表单通过ajax实现无刷新提交

    //将form转换为AJAX提交 function ajaxSubmit(url,frm,fn){ var dataPara=getFormJson(frm); $.ajax({ url:url, t ...

  3. C#实现用Newtonsoft.Json 4.5.11解析人人网院校信息并获取院系信息

    http://cfanz.cn/index.php?c=article&a=read&id=42444 http://files.cnblogs.com/lyl6796910/Wind ...

  4. DBA_实践指南系列10_Oracle Erp R12诊断功能Diagnostic(案例)

    2013-12-10 Created By BaoXinjian Thanks and Regards

  5. [Android实例] 拖动滑块进行图片拼合验证方式的实现

    该篇文章从eoeAndroid搬迁过来的,原文地址:[Android实例] 拖动滑块进行图片拼合验证方式的实现 现在网站上有各种各样的验证码验证方式,比如计算大小,输入图片内容等,今天在一家网站上看到 ...

  6. Android LinearLayout的android:layout_weight属性

    本文主要介绍Android LinearLayout的android:layout_weight属性意义 android:layout_weight为大小权重,相当于在页面上显示的百分比,它的计算是根 ...

  7. docker lnmp php

    使用 Docker 构建 LNMP 环境 https://segmentfault.com/a/1190000008833012 Docker 快速上手指南 https://segmentfault. ...

  8. Logstash怎么导入csv

    Logstash.Conf input { file { type => "SSRCode" path => "E:/FTPRootWorkSpace/SD/ ...

  9. ipa 发布到stroe

    http://my.oschina.net/joanfen/blog/133642 较详细:http://www.techolics.com/apple/20120401_197_2.html

  10. Java:集合,对列表(List)中的数据(整型、字符串、日期等)进行排序(正序、倒序)的方法;字符串按照整型排序的方法

    1. 要求 对List列表中的数据进行排序(正序.倒序),列表中的数据包括:整型(Integer).字符串(String).日期(Date)等.对于字符串,要求允许对它按照整型进行排序. 2. 实现思 ...