Monday, January 9, 2012

Adding items to Listbox using threading


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

Knowing u r sqlserver is 32 bit or 64 bit

  SELECT @@VERSION
If the version returned is x86 that means it is a 32 bit installation and if it is x64 then it means it is a 64 bit installation.