Found the solution:
Before calling the Compact() method make sure there are not any existing locks on the database.
If it is a file geodatabase then open the gdb directory in Windows Explorer and look for LOCK type files, they end in .lock.
For personal geodatabase there will be a .ldb file in the directory with the same name as the .mdb.
- If you have a layer or table from the geodatabase loaded in the map then you will not be able to remove all locks.
- If another user is accessing the geodatabase then you will not be able to remove all locks.
- If you are using arcobjects to temporarily access the geodatabase then you need to use good practices and close any geodatabase resources when the calling process ends. This includes releasing all COM objects when you are finished with them.
Use the ComReleaser Class in ESRI.ArcGIS.ADF.Connection.Local namespace