Avatar of derek7467
derek7467
 asked on

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

Open in new window


Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ListView1.Items.Clear()
        If Not bw.IsBusy = True Then
            bw.RunWorkerAsync()
        End If
        Label2.Visible = False
end sub

Open in new window


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

Open in new window

Visual Basic.NETActive Directory

Avatar of undefined
Last Comment
it_saige

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Mike Tomlinson

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
it_saige

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
derek7467

ASKER
Cool that worked:

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

Open in new window

SOLUTION
it_saige

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck