0

I wrote a quite simple c# app that scans our active directory for locked out accounts and returns the usernames to a list box (LB1). Here is the code for the search. It falls under the Button_Click event:

            try
        {
            PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "DOMAIN", "OU=OUnAme,DC=DOMAIN,DC=com");

            UserPrincipal qbeUser = new UserPrincipal(ctx);
            qbeUser.Enabled = true;

            PrincipalSearcher search = new PrincipalSearcher(qbeUser);           

            foreach (UserPrincipal user in search.FindAll())
            {
                if (user.IsAccountLockedOut())
                {
                    LB1.Items.Add(user.SamAccountName.ToString());
                }
            }
        }

Is there a way to implement a progress bar that fills for the FindAll() event? Would I just want to do a count function first, to determine the max value for the progress bar and then add an incrament as the first step of the foreach loop?

Thank you, Wes

Wes
  • 290
  • 3
  • 18
  • 2
    Don't have time to give a good full answer, but you probably want to look into the [`BackgroundWorker`](http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker%28v=vs.110%29.aspx) component so you can perform the processing without blocking the UI thread. Also keep in mind [this question](http://stackoverflow.com/questions/661561/how-to-update-the-gui-from-another-thread-in-c) which details how to update the UI thread from a background thread. – Anthony Dec 03 '14 at 19:04

2 Answers2

0

use this

UserPrincipal []user_all = search.FindAll();
int max = user_all.length;
progressBar1.Maximum = max;
progressBar1.Step = 1;
foreach (UserPrincipal user in user_all)
{
    if (user.IsAccountLockedOut())
    {
        LB1.Items.Add(user.SamAccountName.ToString());
    }
    progressBar1.PerformStep();
}
  • The only issue I see with this is that it is then scanning AD twice. At that point, it renders the progress bar useless as I might have well just had it scan for the original purpose. – Wes Dec 03 '14 at 19:27
0

General workflow I would do in this situation since you can't get progress feedback from FindAll.

  • Make your progress bar show empty (no progress filled)
  • Have a status that says "Gathering accounts" (or whatever you want)
  • Set FindAll() results to a local variable and get your count.
  • Configure the progress bar to the count plus some arbitrary number to account for the find you just did.
  • Increase the progress to that number you made to show it is completed
  • Now start looping and updating the progress bar.

Don't forget to do this in a background thread so you can actually do work and update the UI at the same time.

TyCobb
  • 8,909
  • 1
  • 33
  • 53