Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem using GDI+ with multiple threads (VB.NET)

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.

like image 829
Joe B Avatar asked Aug 24 '26 23:08

Joe B


1 Answers

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

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

like image 123
Henk Holterman Avatar answered Aug 26 '26 13:08

Henk Holterman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!