The timer doesn't seem to go off (until I hide the window)

This is the image of the design window:

enter image description here

Here is the file 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
    }
}

      

Here is the file 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 text into current time of day
            staticClock.Text = "Current time of day: " +
                DateTime.Now.Hour.ToString() + " : "  +
                DateTime.Now.Minute.ToString() + " : " +
                DateTime.Now.Second.ToString();
        }
    }
}

      

Timer control has the following properties set by the designer:

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

      

As for the label, properties with a designer are also set here:

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

      

Problem:

When I run the application (in debug mode), a window appears with the controls correctly placed and with the correct appearance. Every other piece of code works successfully (opening / drawing a picture, etc.), but the label remains empty as originally set in the designer.

After I minimize / maximize the window, the label text is set correctly. I tried to move the "out" part of the window on the screen and bring it back to see what happens. The text in the label was changed sometimes -> it did not update correctly.

MY EFFORTS TO SOLVE THE PROBLEM:

This is my first time trying C # and WinForms, so I tried to find some online documentation on timers.

After examining the file, .Designer.cs

I found that the timer from the toolbox belongs to the class System.Windows.Forms.Timer

. I didn't find anything to help me, as the Remarks section states that setting the property Enabled

to true

starts the timer, and setting it to false

stops it.

I tried to put a simple message box and it started to appear normally when the window was minimized. When the window is in normal state, nothing is displayed, but other parts of the program work well (open / draw / etc).

After trying Google for a solution / search here on SO, I didn't find a specific solution (although some suggestions were made, but as I said, they didn't work for me).

Question:

How can I change the tick timer handler so that the label text can be changed every second?

What am I doing wrong?

+3


source to share


1 answer


As Hans Passant pointed out in one of his comments, “Paint event handlers should only paint, they should never change properties that cause the Paint event to fire again. This kind of manipulation causes the UI thread to burn 100% of the core, never getting low-priority synthesized messages to send. . Like WM_TIMER. Minimizing the window temporarily stops this. "



+1


source







All Articles