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

I think it would be better if I just copied and pasted the code (this is 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

Exception: “The object is currently being used elsewhere,” occurs during the tick event. Can someone tell me why this is happening and how to solve it? Thank.

+3
source share
1 answer

Handles timer.Elapsed stands for System.Timers.Timer.

Use System.Windows.Forms.Timer instead, and your problem can no longer happen.

+3
source

Source: https://habr.com/ru/post/1741680/


All Articles