Click here to Skip to main content
15,867,594 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I try to create a countdown in c#, but it refreshes the Form UI every second, and this action makes a flashing issue.

i attached this video on youtube to show the issue here

UI Issue with Timer in C# - YouTube[^]

What I have tried:

I tried to use Timer also BackgroundWorker and the issue appears with all of them.

this the last code i tried:

System.Threading.Timer timer;
       private void StartTimer_Click(object sender, EventArgs e)
       {
           timer = new System.Threading.Timer(_ => TimerTicks(), null, 0, 1000);

       }


private void TimerTicks()
        {
            if (selectedQuestTimer > 0)
            {
                if (selectedQuestTimer == selectedQuestion.time)
                {
                    TmerStart.Play();
                }
                if (selectedQuestTimer == selectedQuestion.time - 3)
                {
                    TmerStart.Stop();
                    TmerTicker.Play();
                }
                if (selectedQuestTimer == 5)
                {
                    TmerTicker.Stop();
                    TmerEnd.Play();
                }

                Invoke((MethodInvoker)(() => ChangeTimerDataData()));


                
            }
            else if (selectedQuestTimer == 0)
            {

                //QuestionTimer.Stop();
            }
        }
Posted
Updated 30-Jan-23 1:06am
v2
Comments
Maciej Los 30-Jan-23 8:24am    
What does "ChangeTimerDataData" method?
Dave Kreskowiak 30-Jan-23 9:33am    
The problem is going to be in the code your timer is calling, which you didn't show.

I can't see what your render is doing. You may need to use double buffering...

First try setting the form's DoubleBuffered property to True. Otherwise, for controls, have a read of this: c# flickering Listview on update[^]

You can make that code more generalized for all controls:
C#
public static class ControlsExtension
{
    public static void SetDoubleBuffered(this Control control, bool doubleBuffered = true)
    {
        control
            .GetType()
            .GetProperty("DoubleBuffered", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic)
            ?.SetValue(control, doubleBuffered, null);
    }
}
 
Share this answer
 
Comments
Golden Basim 30-Jan-23 7:44am    
i tried this solution but still flickering the form
Graeme_Grant 30-Jan-23 9:17am    
The code posted is not the issue. Without seeing the rendering code, I can only suggest based on what is a common issue.
Ralf Meier 30-Jan-23 12:50pm    
I agree with @Grame_Grant - somewhere in your code (perhaps the Countdown-Display) forces an Invalidate of the complete Form.
Maybe System.Windows.Forms.Timer will fix your issues?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900