首頁 > 軟體

WinForm中Application.Idle方法詳解

2022-03-01 10:00:46

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;
        }
    }
}

到此這篇關於WinForm中Application.Idle方法的文章就介紹到這了。希望對大家的學習有所幫助,也希望大家多多支援it145.com。


IT145.com E-mail:sddin#qq.com