vb.net and query ldap inside of a background worker
I am attempting to query an LDAP group and return fields to a listview based on that group that i supply in my query. It takes about 15 seconds to run so i would like to run this query in a background worker. I get an error that i cant run a listview via cross-threading. Can someone take a look at my code and help?
Private bw As BackgroundWorker = New BackgroundWorker Public Sub New() InitializeComponent() bw.WorkerReportsProgress = True bw.WorkerSupportsCancellation = True AddHandler bw.DoWork, AddressOf BackgroundWorker1_DoWork AddHandler bw.ProgressChanged, AddressOf BackgroundWorker1_ProgressChanged AddHandler bw.RunWorkerCompleted, AddressOf BackgroundWorker1_RunWorkerCompleted End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.ClickListView1.Items.Clear() If Not bw.IsBusy = True Then bw.RunWorkerAsync() End If Label2.Visible = Falseend sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork Dim worker As BackgroundWorker = CType(sender, BackgroundWorker) Dim userIds As String() = txtcc.Text.Split(New String() {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries) For Each i As String In userIds Dim de As New DirectoryEntry("LDAP://test.net/DC=test,DC=net") Dim LdapFilter As String = "(departmentNumber=" & i & ")" Dim searcher As New DirectorySearcher(de, LdapFilter) Dim result As SearchResult Dim res As SearchResultCollection = searcher.FindAll() For Each result In res Dim item As ListViewItem = ListView1.Items.Add(i) item.SubItems.Add(result.Properties("givenName")(0).ToString()) item.SubItems.Add(result.Properties("cn")(0).ToString()) item.SubItems.Add(result.Properties("userPrincipalName")(0).ToString()) worker.ReportProgress(0, New ListViewState() With {.Item = item, .Group = ListViewGroupDefinition.None}) Next Next End Sub Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged If Not e.UserState Is Nothing AndAlso TypeOf e.UserState Is ListViewState Then Dim state As ListViewState = TryCast(e.UserState, ListViewState) If Not state Is Nothing Then Select Case state.Group Case ListViewGroupDefinition.None Exit Select Case ListViewGroupDefinition.Applications Exit Select End Select ListView1.Items.Add(state.Item) End If End If End Sub Public Class ListViewState Public Property Item() As ListViewItem Public Property Group As ListViewGroupDefinition End Class Public Enum ListViewGroupDefinition As Integer None = 0 End Enum
Any idea, why my label2 doesnt stay until the background worker is done?:
Below is my query button that i put some status text on the page doing the query:
Label2.Visible = True Label2.Text = "Please wait, loading..." delay(2000) ListView1.Items.Clear() If Not bw.IsBusy = True Then bw.RunWorkerAsync() End If Label2.Visible = False
Any idea, why my label2 doesnt stay until the background worker is done?:
Below is my query button that i put some status text on the page doing the query:
Open in new window