Click here to Skip to main content
15,915,093 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I write a simple code with Timer. but when I start timer and then when I try to move form with mouse dawn or when I try to click a Button response with delayed.

What I am doing Wrong ?

What I have tried:

VB
Public Class Form3
    Dim _RGB As Integer
    Dim renk As Color
    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        Dim hiz As Double
        hiz = -0.4

        For i = 256 To 0 Step hiz
            _RGB = RGB(i, 0, 0) 'VScrollBar1.Value, 0, 0) ', VScrollBar2.Value, VScrollBar3.Value)
            renk = ColorTranslator.FromOle(_RGB)
            'RectangleShape1.FillColor = renk
            PictureBox1.BackColor = renk
            PictureBox1.Refresh()
        Next i

        For i = 256 To 0 Step hiz
            _RGB = RGB(0, i, 0) 'VScrollBar1.Value, 0, 0) ', VScrollBar2.Value, VScrollBar3.Value)
            renk = ColorTranslator.FromOle(_RGB)
            'RectangleShape1.FillColor = renk
            PictureBox2.BackColor = renk
            PictureBox2.Refresh()
        Next i

        For i = 256 To 0 Step hiz
            _RGB = RGB(0, 0, i) 'VScrollBar1.Value, 0, 0) ', VScrollBar2.Value, VScrollBar3.Value)
            renk = ColorTranslator.FromOle(_RGB)
            'RectangleShape1.FillColor = renk
            PictureBox3.BackColor = renk
            PictureBox3.Refresh()
        Next i



    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        If Timer1.Enabled = False Then
            Timer1.Start()
        Else
            Timer1.Stop()
        End If
    End Sub
End Class
Posted
Updated 27-Jan-20 16:00pm

Your tight loops are preventing the user interface from responding.
Regularly use the Application.DoEvents Method (System.Windows.Forms) | Microsoft Docs[^]
 
Share this answer
 
Quote:
What I am doing Wrong ?

Your code is executing in the GUI thread, it means that when Timer1_Tick is running, anything else will wait until the end of tick.
Note that setting the speed of animation by artificially adding some workload is bot a good idea either, it worsen the problem.

What is the reason of Timer1_Tick the way you did it ?
 
Share this answer
 
v2
Inside every for - next loop use the application.doevents, So you would need to put it in 3 times.
For i = 256 To 0 Step hiz
          Application.Doevents
         _RGB = RGB(i, 0, 0) 'VScrollBar1.Value, 0, 0) ', VScrollBar2.Value, VScrollBar3.Value)
            renk = ColorTranslator.FromOle(_RGB)
            'RectangleShape1.FillColor = renk
            PictureBox1.BackColor = renk
            PictureBox1.Refresh()
        Next i

This allows the system to monitor for mouse movements or clicks or other needed to respond events. You do not show how long your timer.ticks are for. You may also consider putting a longer time between ticks to also allow the system to respond. Depending on how long this takes to run, and the length of you timer ticks, I would also add timer.stop as the first line of the event, and add timer.start as the last line. This will prevent multiple events running, and really using up the resources.

Application.doevents can have draw backs in some instances, I do not see how this would be one.

Another approach would be to use a separate thread for your timer events. But, that would depend on the overall desired affect and the size of the program.
 
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