C# - COMException was unhandled

1

I am developing an application where I get information about users from Active Directory. I go through each user and get its information. I have a lot of users in the AD and when I run the application at first it works just fine, but when it comes to the user 2000 the application stopps and gives me the error: The handle is invalid. (Exception from HRESULT: 0x80070006 (E_HANDLE))

I don't really know what it means because the exact same application works perfectly fine on another computer and it goes through each user in the AD and gets their information.

What can I do?

EDIT: I use BackgroundWorker to get the information about the users from the AD.

   private void BGWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        try
        {
            //Here I call the method to get user information from the AD.
            GetUserFromADMethod("Server", "AD");
        }
        finally
        {
           //Calling Dispose() here dose not work.
            BGWorker.Dispose();
        }
   }
c#
active-directory
asked on Stack Overflow Mar 16, 2011 by Erik • edited Mar 16, 2011 by Erik

1 Answer

2

It is a low-level Windows error, ERROR_INVALID_HANDLE, error 6. Getting invalid handle values when you enumerate large amounts of data suggests that your app is not calling Dispose() when it should. One possible diagnostic is TaskMgr.exe, Processes tab. View + Select Columns and tick "Handles". Observe the displayed value for your program while it runs. The big kaboom happens when it reaches 10,000 on most machines.

Review your code for proper use of the using statement or calling Dispose() or Close() explicitly when a class you use implements it.

answered on Stack Overflow Mar 16, 2011 by Hans Passant

User contributions licensed under CC BY-SA 3.0