最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

WinForm中Application.Idle方法詳解

 更新時(shí)間:2022年03月01日 08:57:57   作者:.NET開(kāi)發(fā)菜鳥(niǎo)  
本文詳細(xì)講解了WinForm中的Application.Idle方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

Application.Idle()方法表示:當(dāng)應(yīng)用程序處于空閑狀態(tài)時(shí)執(zhí)行相應(yīng)代碼。

示例程序

1、界面設(shè)計(jì):一個(gè)簡(jiǎn)單的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);
            //到達(dá)定時(shí)時(shí)間的時(shí)候執(zhí)行的事件
            timer.Elapsed += new System.Timers.ElapsedEventHandler(TimeUp);
            //設(shè)置是執(zhí)行一次(false) 還是一直執(zhí)行(true)
            timer.AutoReset = true;
            //是否執(zhí)行System.Timers.Timer.Elapsed事件
            timer.Enabled = true;
            //啟動(dòng)
            timer.Start();

        }

        /// <summary>
        /// 定時(shí)到點(diǎn)執(zhí)行的事件
        /// </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 = "進(jìn)入空閑期";
            string strPath = Application.StartupPath + @"test.txt";
            using (StreamWriter sw = new StreamWriter(strPath, true))
            {
                sw.WriteLine("開(kāi)始進(jìn)入空閑期,當(dāng)前時(shí)間:" + DateTime.Now);
                sw.Close();
            }
        }

        private void InitRefresh()
        {
            //設(shè)定IDLE自動(dòng)結(jié)束
            Application.Idle += new EventHandler(OnApplicationIdle);
            //設(shè)定消息過(guò)濾
            FormMessageFilter MessageFilter = new FormMessageFilter();
            MessageFilter.ApplicationActive += new EventHandler(OnApplicationActive);
            Application.AddMessageFilter(MessageFilter);

        }

        /// <summary>
        /// 程序進(jìn)入空閑時(shí)期時(shí)會(huì)一直執(zhí)行此事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnApplicationIdle(object sender, EventArgs e)
        {
            if (timer != null)
                timer.Start();
        }

        /// <summary>
        /// 當(dāng)鍵盤(pán)及鼠標(biāo)事件,關(guān)閉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 = "結(jié)束空閑期,進(jìn)入活動(dòng)期";
            string strPath = Application.StartupPath + @"test.txt";
            using (StreamWriter sw = new StreamWriter(strPath,true))
            {
                sw.WriteLine("開(kāi)始進(jìn)入活動(dòng)期,當(dāng)前時(shí)間:" + DateTime.Now);
                sw.Close();
            }

        }

        
    }

    public class FormMessageFilter : IMessageFilter
    {
        public event EventHandler ApplicationActive;

        /// <summary>
        /// 只要是按鍵盤(pán)及鼠標(biāo)便會(huì)引發(fā)事件
        /// 因?yàn)槭菫榱吮O(jiān)視鍵盤(pán)及鼠標(biāo),所以均return false;
        /// return ture:會(huì)把輸入的值清除
        /// 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;
        }
    }
}

到此這篇關(guān)于WinForm中Application.Idle方法的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

手机| 柘荣县| 盐边县| 西乡县| 黑河市| 永胜县| 阿图什市| 文水县| 兴业县| 云南省| 怀远县| 崇阳县| 红桥区| 台北县| 三原县| 五寨县| 郓城县| 贡嘎县| 张家川| 蓬溪县| 周至县| 思南县| 基隆市| 兖州市| 金阳县| 合阳县| 重庆市| 孟连| 彩票| 德阳市| 宁乡县| 辰溪县| 汾阳市| 惠水县| 五大连池市| 西乌珠穆沁旗| 策勒县| 土默特右旗| 永川市| 万盛区| 香港|