views:

105

answers:

1

I think it would be best if I just copy and pasted the code (it's very trivial).

Private Sub Main() Handles MyBase.Shown
    timer.Interval = 10
    timer.Enabled = True
End Sub

Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
    e.Graphics.DrawImage(image, 0, 0)
End Sub

Private Sub tick() Handles timer.Elapsed
    Using g = Graphics.FromImage(image)
        g.Clear(Color.Transparent)
        g.DrawLine(Pens.Red, 0 + i, 0 + i, Me.Width - i, Me.Height - i)
    End Using

    Me.Invalidate()
End Sub

An exception, "The object is currently in use elsewhere", is raised during the tick event. Could someone tell me why this happens and how to solve it? Thanks.

+3  A: 

The Handles timer.Elapsed indicates a System.Timers.Timer.

Use a System.Windows.Forms.Timer instead and your problem cannot happen anymore.

Henk Holterman
Problem solved. Thank you very much!
Joe B
Good answer. Just pointing out the forms timer synchronizes with it's owner control thread, whereas the system timer you had to manually synchronize before calling cross-thread objects.
Wez