Reputation: 95
I get an error about cross-threading when I try to add an item to a listbox. This Sub is located inside the frmMain class. lstSells is a listbox on frmMain. Its gotta be a simple fix but 20+ goods searches later and tons of web pages I'm not seeing it. People say they have problems when trying to do it from a different class, but not like this. I am fairly new to vb.net, used to us VB6 a fair bit, so maybe it's something simple I'm missing here?
Private Sub OnMessage(sender As Object, e As MessageEventArgs)
Messages.Add(e.Data)
lstSells.Items.Add("test")
End Sub
Upvotes: 0
Views: 1406
Reputation: 12222
The problem fires when you try to update the form from another Thread, not necessary from another class.
See how to call delegates on MSDN, they really helps! http://msdn.microsoft.com/en-us/library/zyzhdc6b.aspx
Upvotes: 2
Reputation: 20464
Try this:
If lstSells.InvokeRequired Then
lstSells.Invoke(Sub() lstSells.Items.Add("test"))
Else
lstSells.Items.Add("test")
End If
Upvotes: 1