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. H5版如何在微信外(非微信浏览器)进行微信支付技术方案

    官方是支持在非微信内置浏览器中调起微信支付的!H5支付是基于公众号基础开发的一种非微信内浏览器支付方式(需要单独申请支付权限),可以满足在微信外的手机H5页面进行微信支付的需求.同时,由于H5链接传播 ...

  2. Axure多人协作

    这几天搞<材料採购系统>需求.我们组须要分模块画模型图,可是假设每一个人各自画各自的,最后整合,这样就太麻烦了.小左说Axure能够实现多人协作,于是我就研究了一下.我们组已经在用了.以下 ...

  3. VC编译错误: Nafxcwd.lib(dllmodul.obj) : error LNK2005: _DllMain@12已经在dllmain.obj 中定义

    错误: Nafxcwd.lib(dllmodul.obj) : error LNK2005: _DllMain@12已经在dllmain.obj 中定义 解决: 打开项目属性对话框, C/C++ -& ...

  4. VirtualBOX 不能mount优盘,移动硬盘解决方案

    The Solution (basically nayasis' solution with the second driver installation added): Safely unplug ...

  5. Android开发14——监听内容提供者ContentProvider的数据变化

    一.提出需求 有A,B,C三个应用,B中的数据需要被共享,所以B中定义了内容提供者ContentProvider:A应用修改了B应用的数据,插入了一条数据.有这样一个需求,此时C应用需要得到数据被修改 ...

  6. TreeMap升序|降序排列和按照value进行排序

    TreeMap 升序|降序排列 import java.util.Comparator; import java.util.TreeMap; public class Main { public st ...

  7. MYSQL加入远程用户或同意远程訪问三种方法

    加入远程用户admin密码为password GRANT ALL PRIVILEGES ON *.* TO admin@localhost IDENTIFIED BY \'password\' WIT ...

  8. 利用vagrant打包系统--制作自己的box

    前置条件1.安装VirtualBox 2.安装Vagrant 3.在VirtualBox中安装操作系统,例如 CentOS 1)把在virtualBox中安装的系统打包成box 1.我们需要知道虚拟机 ...

  9. Vim下的插件管理工具pathogen简介

    1.pathogen简介:    通常情况下安装vim插件是将所有的插件和相关的doc文件都安装在一个文件夹中,如$VIM/vim74/plugin目录下,文档在$VIM/vim74/doc目录下,但 ...

  10. coreos 创建使用密钥登陆的ubuntu 基础镜像

    下载官方镜像 core@localhost ~ $ docker pull ubuntu:14.04 #假设官方下载较慢,可到www.dockerpool.com下载标准镜像 core@localho ...