服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - WinForm中Application.Idle方法详解

WinForm中Application.Idle方法详解

2023-02-16 14:47.NET开发菜鸟 C#

本文详细讲解了WinForm中的Application.Idle方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

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

示例程序

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

WinForm中Application.Idle方法详解

2、代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
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;
        }
    }
}

到此这篇关于WinForm中Application.Idle方法的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://www.cnblogs.com/dotnet261010/p/6837870.html

延伸 · 阅读

精彩推荐
  • C#C# ref and out的使用小结

    C# ref and out的使用小结

    这篇文章主要介绍了C# ref and out的使用小结,帮助大家更好的理解和学习使用c#,感兴趣的朋友可以了解下...

    LeeMacrofeng7132022-11-04
  • C#C#9新特性之增强的模式匹配

    C#9新特性之增强的模式匹配

    这篇文章主要介绍了C#9新特性之增强的模式匹配,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面...

    WeihanLi5432022-10-26
  • C#c#图片上传和显示的实现方法

    c#图片上传和显示的实现方法

    这篇文章主要介绍了c#实现图片上传和显示的实现方法,可实现图片上传效果预览功能,需要的朋友可以参考下。...

    叶超Luka8922021-12-08
  • C#C#处理猜拳问题的简单实例(非窗体)

    C#处理猜拳问题的简单实例(非窗体)

    下面小编就为大家带来一篇C#处理猜拳问题的简单实例(非窗体)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    C#教程网9692021-12-01
  • C#C#编程调用Cards.dll实现图形化发牌功能示例

    C#编程调用Cards.dll实现图形化发牌功能示例

    这篇文章主要介绍了C#编程调用Cards.dll实现图形化发牌功能,结合实例形式分析了C#动态链接库调用及图形操作技巧,需要的朋友可以参考下...

    songkexin3502022-01-11
  • C#unity shader 较完整光照(含有多光源阴影)

    unity shader 较完整光照(含有多光源阴影)

    这篇文章主要介绍了unity shader 较完整光照(含有多光源阴影),本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,...

    学者(cloudea)4682022-12-05
  • C#C#中DataTable 转实体实例详解

    C#中DataTable 转实体实例详解

    这篇文章主要介绍了C#中DataTable 转实体实例详解,需要的朋友可以参考下...

    Langu10602021-12-31
  • C#使用C#写了一个可以推算火车票身份证号码的小程序

    使用C#写了一个可以推算火车票身份证号码的小程序

    这篇文章主要介绍了使用C#写了一个可以推算火车票身份证号码的小程序 的相关资料,需要的朋友可以参考下...

    葛云飞6082021-11-11