c# - 计时器似乎没有滴答声(直到我最小化窗口)

标签 c# .net winforms timer

这是设计窗口的图像:

enter image description here

这是 MainForm.Designer.cs 文件:

namespace SamsCSharp24
{
    partial class ImeObrasca
    {
        // irrelavent code is omitted, only event subscriptions are left
        private void InitializeComponent()
        {
            // irrelavent code is omitted for brewity
            // 
            // SelectPicture
            // 
            this.SelectPicture.Paint += new System.Windows.Forms.PaintEventHandler(this.SelectPicture_Paint);
            this.SelectPicture.Click += new System.EventHandler(this.SelectPicture_Click);
            // 
            // Quit
            // 
            this.Quit.Click += new System.EventHandler(this.Quit_Click);
            // 
            // PictureBox
            //
            this.PictureBox.MouseLeave += new System.EventHandler(this.PictureBox_MouseLeave);
            this.PictureBox.MouseEnter += new System.EventHandler(this.PictureBox_MouseEnter);
            // 
            // btnOptions
            //
            this.btnOptions.Click += new System.EventHandler(this.btnOptions_Click);
            // 
            // timerClock
            // 
            this.timerClock.Tick += new System.EventHandler(this.timerClock_Tick);
        }

        #endregion
    }
}

这是 MainForm.cs 文件:

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 SamsCSharp24
{
    public partial class ImeObrasca : Form
    {
        public ImeObrasca()
        {
            InitializeComponent();
            // when uncommenting below line, window is not seen in taskbar
            // this.ShowInTaskbar = false; 
        }

        // other code is omitted for brewity 
        private void SelectPicture_Paint(object sender, PaintEventArgs e)
        {
            // just for fun, change color of a button to light blue
            SelectPicture.BackColor = Color.Azure;
        }

        private void timerClock_Tick(object sender, EventArgs e)
        {
            // when timer ticks, change label's text into current time of day
            staticClock.Text = "Current time of day: " +
                DateTime.Now.Hour.ToString() + " : "  +
                DateTime.Now.Minute.ToString() + " : " +
                DateTime.Now.Second.ToString();
        }
    }
}

计时器控件具有通过设计器设置的以下属性:

Enabled = true;
Interval = 1000
Name = timerClick
Tick (event) = timerClock_Tick

对于label,这里也是用designer设置的属性:

BorderStyle = FixedSingle
Name = staticClock
Autosize = false
Text = 
Other properties are default or irrelevant ( like Location or Size )

问题:

当我运行应用程序时(在 Debug模式下),窗口会显示正确放置的控件和正确的外观。代码的所有其他部分都成功运行(图片打开/绘图等),但标签仍为空,如最初在设计器中设置的那样。

在我最小化/最大化窗口后,标签文本设置正确。我试图将带有标签“out”的窗口部分移出屏幕,然后将其取回以查看会发生什么。标签中的文本有时已更改 -> 更新不正确。

我为解决问题所做的努力:

这是我第一次尝试 C# 和 WinForms,所以我试图找到一些关于计时器的在线文档。

检查 .Designer.cs 文件后,我发现工具箱中的计时器属于 System.Windows.Forms.Timer 类。我在那里找不到任何可以帮助我的东西,因为在 Remarks 部分指出将属性 Enabled 设置为 true 启动计时器,并将其设置为 false 停止它。

我尝试放置简单的消息框,当窗口最小化时它开始正常弹出。当窗口处于正常状态时没有任何显示,但程序的其他部分运行良好(图片打开/绘图/等)。

在尝试通过 Google 寻找解决方案/在此处搜索 SO 后,我没有找到具体的解决方案(虽然提出了一些建议,但正如我所说,它们对我没有帮助)。

问题:

如何修改计时器的滴答处理程序,以便每秒修改标签的文本?

我做错了什么?

最佳答案

正如 Hans Passant 在其中一条评论中所述,“Paint 事件处理程序应该只进行绘制,它们不应该更改导致 Paint 事件再次被触发的属性。这种恶作剧导致 UI 线程消耗 100% 的核心,永远不会得到调度低优先级合成消息。像 WM_TIMER。最小化窗口会暂时停止它。”

关于c# - 计时器似乎没有滴答声(直到我最小化窗口),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25556413/

相关文章:

.net - 如何使用 IHostingEnvironment

c# - 如何使非模态窗体居中

c# - WinForms(Windows 窗体)的等效 JDialog 是什么?

c# - Couchbase N1QL 结果根据 SELECT 语法改变形状

c# - OpenXML:用 WordprocessingML 中的表替换 <sdt/> 元素

c# - C# 的浮点比较函数

c# - 从C#中嵌入的浏览器控件获取HTML源代码

.net - 读取 WebException 的响应流时出现 WebException

c# - .net WinForms 控件验证 - 如何显式验证所有控件

c# - ASP.NET Core 2.1 将 json 列表传递给 ajax