C#请完成我想要的timer控件功能,讲思路也行,给代码再加分!

2025-05-16 15:22:51
推荐回答(2个)
回答1:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        //循环次数
        private int i; 
        //分钟数
        private int j; 
        //秒数
        private int seconds;

        public Form2()
        {
            InitializeComponent();
            timer1.Interval = 1000; //定时1秒
        }

        private void button4_Click(object sender, EventArgs e)
        {
            // 启动循环处理期间,禁止点击这个按钮
            button4.Enabled = false; 
            // 循环处理
            for (int k = 0; k < i; k++)
            {
                //执行一次你的操作
                YourJob();
                //防止长时间循环冻结窗口
                Application.DoEvents(); 
            }
            // 循环结束后,启动延时定时器
            seconds = 0;
            timer1.Start();
        }

        
        private void YourJob()
        {
            //你的操作
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            // 增加秒数
            seconds++;
            if ((seconds % 60) == 0)
            {
                // 延时60秒
                j--;
            }
            // 延时达到规定的分钟数
            if (j == 0)
            {
                // 停止定时器
                timer1.Stop();
                // 使能按钮,允许再次操作
                button4.Enabled = true;
            }
        }
    }
}

回答2:

在timer1_Tick结尾增加sleep()