dcsimg
Results 1 to 3 of 3

Thread: While timer working, Why Form can not move properly ?

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2018
    Posts
    47

    While timer working, Why Form can not move properly ?

    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 ?

    Code:
    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

  2. #2
    Junior Member
    Join Date
    Nov 2019
    Posts
    24

    Re: While timer working, Why Form can not move properly ?

    Hi. You may want to try inserting the following within the recursive loops -
    "My.Application.DoEvents()"
    I believe this allows other pending tasks to be performed concurrently.

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    34,918

    Re: While timer working, Why Form can not move properly ?

    It will, but it still likely won't do what you want it to do.

    If you didn't have those Refresh calls in there, those loops would happen so fast you'd never see anything. By adding the refresh statements, you are forcing the control to draw right then, rather than the "whenever I get around to it" that would normally happen. So, without the refresh, your loops would run to completion, then the screen would update one time with any changes. By adding the refresh in there, the loops still run to completion, they just redraw the control every time they hit a refresh. The method never relinquishes control, though, so no user actions can be performed because the UI thread is busy with all that drawing until the loops have all finished. At that time, the form may respond, but only until the timer ticks again.

    DoEvents inside each loop will allow the form to respond to user actions every time through the loop, which may well be good enough, since I don't know what you are trying to achieve, ultimately, but DoEvents can have some strange costs, especially if the timer interval is short. If the timer tick is short, then it could tick again while the first tick is being handled. DoEvents would then allow the messages on the queue to be processed...which would include the Tick event, so you'd re-enter the tick event handler. If nothing too bad happened, then eventually the stack would overflow. Of course, that won't happen if the ticks are long enough, but other things could happen.

    A better solution would be to rethink what happens in the tick. Those loops are pointless, as they are written. If you take out the refresh, you'd never see any of the intermediate steps. With the refresh, you'll see them, but as fast as the screen can draw (it might even skip some). Instead, consider removing the loops. Each time the timer ticks, make that one iteration of one loop without the refresh. Your timer tick might be just this:

    Code:
    _RGB = RGB(i, 0, 0) 'VScrollBar1.Value, 0, 0) ', VScrollBar2.Value, VScrollBar3.Value)
                renk = ColorTranslator.FromOle(_RGB)
                'RectangleShape1.FillColor = renk
                PictureBox1.BackColor = renk
    Then let that happen at whatever pace you want. That would be fast enough that you could use an interval of 100, or even 50, but 50 would mean that it would be drawing 20 times a second, which would just look like a flicker to most people.

    Also, I didn't know you even COULD decrement a loop by a double value. I'm not sure that you really are anyways, which could be causing a different problem. There's no reason in that code that the loops are decrementing rather than the standard incrementing, and no clear reason why to decrement by a double, but I assume that the code once did something other than what you are showing currently.

    Whatever you intended, the problem you are seeing is that the tick is taking too long, which is preventing the UI from responding to user input. As written, the loops are useless, but I assume that what you posted is not what you are trying to do, but some code intended to study some issue. Therefore, it would ultimately be best to get rid of the loops and use the ticks of the timer as your loop.
    My usual boring signature: Nothing

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width