2
回答
C#在timer_Tick中时间比较不能触发代码
利用AWS快速构建适用于生产的无服务器应用程序,免费试用12个月>>>   

代码中的if语句无法触发

 private void timer2_Tick(object sender, EventArgs e)
        {
            //提前5中提醒,5分钟后强制关机
            DateTime test = DateTime.Now.AddMinutes(5);
            TimeSpan subTime_T = test - myVar.dtVar;
            TimeSpan subTime_N = DateTime.Now - myVar.dtVar;
            if (subTime_T.Hours==0&&subTime_T.Minutes==0)
            {
                this.Visible = true;
                this.WindowState = FormWindowState.Normal;
                this.Show();
                this.notifyIcon1.Visible = false;
                MessageBox.Show("还有10分钟关机", "时间提醒", MessageBoxButtons.OK);
                label1.Text = myVar.dtVar.ToLongTimeString();
            }
           // label1.Text = myVar.dtVar.ToLongTimeString();
        
            //计时完成,关机
            if (subTime_N.Hours==0&&subTime_N.Minutes==0)
            {
                label1.Text = myVar.dtVar.ToLongTimeString();
                DoExitWin(EWX_SHUTDOWN);
            }
        }

        private void notifyIcon1_MouseClick_1(object sender, MouseEventArgs e)
        {
            this.timer1.Enabled = true;
            this.Visible = true;
            this.WindowState = FormWindowState.Normal;
            this.Show();
            this.notifyIcon1.Visible = false;
        }

这是myVar的定义

public Form1()
        {
            InitializeComponent();
            //启动定时器,实现时间实时更新
            this.timer1.Interval = 1000;
            this.timer1.Start();

            //设置默认时间设定
            setMin.SelectedIndex = 0;
            setHour.SelectedIndex = 3;
        }

        public class myVar
        {
            public static DateTime dtVar=DateTime.Parse("0:00:00");
            public static int z = 0;
        }

 

这是myVar的赋值

public void setMin_SelectedIndexChanged(object sender, EventArgs e)
        {
            //获取用户时间设定
            string setMinute=setMin.SelectedItem.ToString();
            int setMinuteInt=int.Parse(setMinute);
           
            //根据用户设定建立提醒时间
            DateTime myTimer =new DateTime(DateTime.Now.Year,DateTime.Now.Month,DateTime.Now.Day,myVar.dtVar.Hour,myVar.dtVar.Minute,0).AddMinutes(setMinuteInt);
            myVar.dtVar = myTimer;
        }

 

举报
kaiyuanheshang
发帖于4年前 2回/895阅
顶部