Johnny Prescott
Johnny Prescott

Reputation: 271

getgroup() is very slow

I am using the function getgroup() to read all of the groups of a user in the active directory.

I'm not sure if I'm doing something wrong but it is very very slow. Each time it arrives at this point, it takes several seconds. I'm also accessing the rest of Active directory using the integrated function of "Accountmanagement" and it executes instantly.

Here's the code:

For y As Integer = 0 To AccountCount - 1
    Dim UserGroupArray As PrincipalSearchResult(Of Principal) = UserResult(y).GetGroups()

    UserInfoGroup(y) = New String(UserGroupArray.Count - 1) {}

    For i As Integer = 0 To UserGroupArray.Count - 1
        UserInfoGroup(y)(i) = UserGroupArray(i).ToString()
    Next
Next

Later on...:

AccountChecker_Listview.Groups.Add(New ListViewGroup(Items(y, 0), HorizontalAlignment.Left))
For i As Integer = 0 To UserInfoGroup(y).Count - 1
    AccountChecker_Listview.Items.Add(UserInfoGroup(y)(i)).Group = AccountChecker_Listview.Groups(y)
Next

Item(,) contains my normal Active directory data that I display Item(y, 0) contain the username.

y is the number of user accounts in AD. I also have some other code for the other information in this loop but it's not the issue here.

Anyone know how to make this goes faster or if there is another solution?

Upvotes: 0

Views: 242

Answers (1)

Phillip Trelford
Phillip Trelford

Reputation: 6543

I'd recommend trying to find out where the time is spent. One option is to use a profiler, either the one built into Visual Studio or a third-party profiler like Redgate's Ants Profiler or the Yourkit .Net Profiler.

Another is to trace the time taken using the System.Diagnostics.Stopwatch class and use the results to guide your optimization efforts. For example time the function that retrieves data from Active Directory and separately time the function that populates the view to narrow down where the bottleneck is.

If the bottleneck is in the Active Directory lookup you may want to consider running the operation asynchronously so that the window is not blocked and populates as new data is retrieved. If it's in the listview you may want to consider for example inserting the data in a batch operation.

Upvotes: 1

Related Questions