Winforms C#Windows.Forms.Timer无法正常工作

Winforms C#Windows.Forms.Timer无法正常工作,winforms,Winforms,在C#/Windows.Forms上制作模拟钟摆的简单应用程序时,我遇到了一些麻烦 它是一个简单的应用程序,所有后端逻辑都以表单代码的形式出现。 首先,我将间隔设置为100毫秒(第60行),但当我计算圈数(第155行)并显示它时,显示消息: 每秒刷新一次(不是每100毫秒刷新一次) 它的行为是这样的:1-2-4-5-6-8或1-3-4-5-7-8等等 我不知道什么时候开始窃听,我想确定错误 非常感谢你的帮助 using System; using System.Drawing; using Sy

在C#/Windows.Forms上制作模拟钟摆的简单应用程序时,我遇到了一些麻烦

它是一个简单的应用程序,所有后端逻辑都以表单代码的形式出现。 首先,我将间隔设置为100毫秒(第60行),但当我计算圈数(第155行)并显示它时,显示消息:

  • 每秒刷新一次(不是每100毫秒刷新一次)
  • 它的行为是这样的:1-2-4-5-6-8或1-3-4-5-7-8等等
  • 我不知道什么时候开始窃听,我想确定错误

    非常感谢你的帮助

    using System;
    using System.Drawing;
    using System.Windows.Forms;
    
    namespace Koban_Lab1
    {
    public partial class mainWindow : Form
    {
        public Point pendFulcrum = new Point(150, 0);
        public Point pendTip = new Point();
        private bool deviceState_;
        private long ticks_;
        private DateTime startTiming;
        private Pen blackPencil;
        public static double g = 9.8;
        public double T;
        public double periodInTicks;
        public double L;
        public double a;
        public double Amp;
        public double startOffset;
        public double w;
        public double periodCounter;
        public double x;
        public double y;
        public double currentAngle;
    
        private Timer timer_;
        public double t;
    
        public Bitmap field;
    
        private long cnt = 0;
        private bool DeviceState
        {
            get
            {
                return deviceState_;
            }
            set
            {
                if (value == deviceState_)
                {
                    return;
                }
    
                deviceState_ = value;
                switch (value)
                {
                    case true:
                        L = Convert.ToDouble(pendulumLengthTextBox.Text) / 100;
                        a = Convert.ToDouble(plummetDistanceTextBox.Text) / 100;
                        periodCounter = 0;
                        T = 2 * Math.PI * Math.Sqrt((L * L) / (12 * g * a) + a / g);
                        w = 2 * Math.PI / T;
                        Amp = 2 * Math.PI * L / 24;
                        startOffset = Amp;
                        startTiming = DateTime.Now;
                        timer_ = new Timer();
                        timer_.Interval = 100;
                        timer_.Tick += new EventHandler(tickTimer);
                        timer_.Start();
                        startPendulumButton.Enabled = false;
                        stopPendulumButton.Enabled = true;
                        pendulumLengthTextBox.Enabled = false;
                        plummetDistanceTextBox.Enabled = false;
                        addReadoutButton.Enabled = true;
                        break;
                    case false:
                        timer_.Stop();
                        timer_ = null;
                        startPendulumButton.Enabled = true;
                        stopPendulumButton.Enabled = false;
                        pendulumLengthTextBox.Enabled = true;
                        plummetDistanceTextBox.Enabled = true;
                        timerTextBox1.Text = Locale.timerDisabledMessage;
                        timerTextBox2.Text = "0";
                        addReadoutButton.Enabled = false;
                        break;
                }
    
                return;
            }
        }
    
        public Graphics fieldGraphics;
    
        public mainWindow()
        {
            InitializeComponent();
        }
    
        private void Form1_Load(object sender, EventArgs e)
        {
            pendulumLengthTooltip.SetToolTip(pendulumLengthTextBox, Locale.pendulumLengthTooltipMessage);
            plummetDistanceTooltip.SetToolTip(plummetDistanceTextBox, Locale.plummetDistanceTooltipMessage);
            blackPencil = Pens.Gray;
            timerTextBox1.Text = Locale.timerDisabledMessage;
            timerTextBox2.Text = "0";
            deleteReadoutButton.Enabled = false;
            DeviceState = false;
            field = new Bitmap(pendulumField.Width, pendulumField.Height);
        }
    
        private void startPendulumButton_Click(object sender, EventArgs e)
        {
            DeviceState = true;
        }
    
        private void stopPendulumButton_Click(object sender, EventArgs e)
        {
            DeviceState = false;
        }
    
        private void addReadoutButton_Click(object sender, EventArgs e)
        {
            if (timerTextBox1.Text == Locale.timerDisabledMessage)
            {
                return;
            }
    
            readoutList.Items.Add(timerTextBox1.Text);
            deleteReadoutButton.Enabled = true;
    
            return;
        }
    
        private void deleteReadoutButton_Click(object sender, EventArgs e)
        {
            if (readoutList.SelectedIndex != (-1))
            {
                readoutList.Items.RemoveAt(readoutList.SelectedIndex);
            }
            else
            {
                readoutList.Items.RemoveAt(readoutList.Items.Count - 1);
            }
    
            if (readoutList.Items.Count == 0)
            {
                deleteReadoutButton.Enabled = false;
            }
        }
    
    
    
        private void tickTimer(object sender, EventArgs e)
        {
            ticks_ = DateTime.Now.Ticks - startTiming.Ticks;
            timerTextBox1.Text = TimeSpan.FromTicks(ticks_).ToString(@"mm\:ss\:ff");
            timerTextBox2.Text = periodCounter.ToString();
    
            ticks_ = DateTime.Now.Ticks - startTiming.Ticks;
            t = ticks_ / TimeSpan.TicksPerSecond;
            periodCounter = t / T;
            timerTextBox2.Text = Convert.ToInt32(periodCounter).ToString();
    
    
            currentAngle = Amp * Math.Sin(w * t);
            x = x * Math.Cos(currentAngle) - y * Math.Sin(currentAngle);
            y = x * Math.Sin(currentAngle) + y * Math.Cos(currentAngle);
    
            Program.labWindow.fieldGraphics = Graphics.FromHwnd(Program.labWindow.pendulumField.Handle);
            pendTip.X = (int)x;
            pendTip.Y = (int)y;
            fieldGraphics.DrawLine(blackPencil, pendFulcrum, pendTip);
    
            pendulumField.Image = field;
            Application.DoEvents();
        }
    
        private void plummetDistanceTooltip_Popup(object sender, PopupEventArgs e)
        {
    
        }
    
        private void plummetDistanceLabel_Click(object sender, EventArgs e)
        {
    
        }
    }
    }
    

    这里有一些要点:1)StackOverflow中没有行号。将代码分成多个部分或添加注释“标签”。2) 计时器是执行周期性操作的一种糟糕方式,因为它们不准确。如果TimerTick代码需要很长时间才能执行,则会扭曲计时。3) 尝试删除与演示问题无关的代码段。4) 你当时对问题的描述不清楚。请更准确和格式更好。我将发布另一个问题,谢谢你,而不是另一个问题,你可以编辑同一个问题