WinForm中Application.Idle方法详解
作者:.NET开发菜鸟 发布时间:2022-09-05 01:53:06
标签:WinForm,Application.Idle,方法
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(120000);
//到达定时时间的时候执行的事件
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;
}
}
}
来源:https://www.cnblogs.com/dotnet261010/p/6837870.html


猜你喜欢
- 前言在之前 LiveData 源码浅析的博客中提到了 ViewModel 组件,当时对 ViewModel 的解释是 “
- 系统自带的VideoView有些视频格式不支持,那么我们可以用第三方实现的VideoView替代系统的来播放视频,比较流行的有ijkplay
- 介绍无论是在WPF中还是WinForm中,都有用户控件(UserControl)和自定义控件(CustomControl),这两种控件都是对
- 1. 类的定义面向对象是通过类和对象去描述和代表万千事物对象的,首先我们需要知道如何去定义一个类。类的组成是由属性和行为两部分组成属性:在类
- 使用官方的刷新控件SwipeRefreshLayout来实现下拉刷新,当RecyclerView滑到底部实现下拉加载(进度条效果用Recyc
- 前言本文主要给大家介绍了关于Spring4自定义@Value功能的相关内容,使用的Spring版本4.3.10.RELEASE,下面话不多说
- 本文实例为大家分享了Qt TCP实现简单通信的具体代码,供大家参考,具体内容如下在.pro文件中添加网络模块 Qt += network服务
- RTF文档即富文本格式(Rich Text Format)的文档。我们在处理文件时,遇到需要对文档格式进行转换时,可以将RTF转为其他格式,
- 目前大多数开发者使用EventBus或者Otto作为事件总线通信库,对于RxJava使用者来说,RxJava也可以轻松实现事件总线,因为它们
- bufferedReader.readLine()读到最后发生阻塞最近在做一个imageserver,需求简化后就是使用socket响应HT
- 高效检索海量信息(经典查找算法)是现代信息世界的基础设施。我们使用符号表描述一张抽象的表格,将信息(值)存储在其中,然后按照指定的键来搜索并
- 不过在实际的工作中,很少会直接用到它。通常都是用的spring-quartz组件,直接通过配置,让spring框架来自动装配如下就是spri
- 本文实例为大家分享了flutter日期时间选择器的具体代码,供大家参考,具体内容如下1 日期选择器 //设置默认显示的日期为当前 DateT
- 在上节使用了H2之后感觉很爽,很轻便,正好有个项目要求简单,最好不适用外部数据库,于是就想着把H2数据库集成进来,这个系统已经存在了一个Or
- 1、心跳机制简介在分布式系统中,分布在不同主机上的节点需要检测其他节点的状态,如服务器节点需要检测从节点是否失效。为了检测对方节点的有效性,
- 这篇文章主要介绍了JAVA泛型的继承和实现、擦除原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的
- activity动画方式在AndroidMenifest中添加activity的动画属性windowAnimationStyle <i
- 近期遇到了DateTime到底是值类型还是引用类型的疑惑,顺势较深入地了解一下DateTime相关的内容结论:DateTime是值类型,因为
- mybatis 报错显示sql中有两个limit使用mybatis进行分页查询时,打印的查询sql中带有两个limit。经过审查:原因是由于
- 双亲委派模型类加载这个概念应该算是Java语言的一种创新,目的是为了将类的加载过程与虚拟机解耦,达到”通过类的全限定名来获取描述此类的二进制