计时器在计算机不活动时启动

时间:2019-10-10 09:58:05

标签: c# winforms timer

要实现此目的,我应该编写什么代码:闲置计时器将在计算机不移动15分钟后启动,并且闲置计时器将在再次移动计算机时停止。

此外,我希望在IDLE计时器启动时停止计时器(活动计时器)。然后,当个人计算机再次移动时,活动计时器将恢复,并且空闲计时器将停止。

这是我到目前为止所做的:

namespace TITOMS_LOGIN
{
    public partial class Form3Admin: Form
    {
        int seconds;
        int minutes;
        int hours;
        public Form3Admin()
        {
            InitializeComponent();
            seconds = minutes = hours = 0;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Hide();
            Form2Admin sw = new Form2Admin();
            sw.Show();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Hide();
            Form5 se = new Form5();
            se.Show();
        }

        private void Form3Admin_Load(object sender, EventArgs e)
        {

            Time.Text = DateTime.Now.ToShortTimeString();
            Day.Text = DateTime.Now.ToLongDateString();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            seconds++;

            if (seconds > 59)
            {
                minutes++;
                seconds = 0;
            }

            if (minutes > 58)
            {

                hours++;
                minutes = 0;

            }

            lblhours.Text = hours.ToString() + "HRS";
            lblminutes.Text = minutes.ToString() + "MINS";
        }

        private void button4_Click(object sender, EventArgs e)
        {
            timer1.Start();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            timer1.Stop();

        }

    }
}

1 个答案:

答案 0 :(得分:0)

private void timer3_Tick(object sender, EventArgs e)
        {
            TimeSpan idleTime = TimeSpan.FromMinutes(0.5);
            TimeSpan aa = TimeSpan.FromSeconds(1);


            if (UserInput.IdleTime >= idleTime && timer1.Enabled)
            {
                timer2.Start();
                timer1.Stop();
                Console.WriteLine("Stopped Timer 1, Start Timer 2 ");

            }
            else if (UserInput.IdleTime < aa && timer2.Enabled)
            {
                timer1.Start();
                timer2.Stop();
                Console.WriteLine("Stopped Timer 2, Start Timer 1 ");
            }
            Console.WriteLine("Idle for " + UserInput.IdleTime.ToString());

        }