In VB.Net windows application ,there is a issue adding items to listbox while there is a timer control .
This issue can be resolved as below
Private Delegate Sub UpdateListInvoker(ByVal StringList As List(Of String))
Private Sub AddStatusLog(ByRef EventText As String)
Dim i As Short
If lstStatusLog.Items.Count > 30000 Then
For i = 1 To 1000
lstStatusLog.Items.RemoveAt(0)
Next i
End If
Dim strval As String = VB6.Format(Now, "long time") & " " & EventText
Dim nThread As New Threading.Thread(AddressOf TestList)
nThread.Start(strval)
sta.Items.Item(1).Text = EventText
End Sub
Private Sub TestList(ByVal EventText As Object)
UpdateList(New List(Of String)(New String() {EventText}))
End Sub
Private Sub UpdateList(ByVal strList As List(Of String))
If Me.lstStatusLog.InvokeRequired Then
Me.lstStatusLog.Invoke(New UpdateListInvoker(AddressOf UpdateList), strList)
Else
Me.lstStatusLog.Items.AddRange(strList.ToArray)
End If
End Sub
No comments:
Post a Comment