Litedb: The process cannot access the file because it is being used by another process.

Created on 15 Jan 2017  路  4Comments  路  Source: mbdavid/LiteDB

I installed the latest pre-released nuget (beta-3) and am getting this error:

An exception of type 'System.IO.FileLoadException' occurred in mscorlib.ni.dll but was not handled in user code.

Additional information: The process cannot access the file because it is being used by another process. (Exception from HRESULT: 0x80070020)

Stack Trace:

"at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n  
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()\r\n   
at BreadPlayer.Service.DatabaseService.<CreateDB>d__9.MoveNext()"

I have this code:

 public virtual async void CreateDB()
        {
            try
            {
                var disk = new FileDiskService(ApplicationData.Current.LocalFolder.Path + @"\breadplayer.db", new FileOptions() { FileMode = FileMode.Exclusive, Journal= true });
                db = new LiteDatabase(disk);                
                    tracks = db.GetCollection<Mediafile>("tracks");
                    playlists = db.GetCollection<Playlist>("playlists");
                    recent = db.GetCollection<Mediafile>("recent");
                    tracks.EnsureIndex(t => t.Title);
                    tracks.EnsureIndex(t => t.LeadArtist);                
            }
            catch(Exception)
            {
                await ApplicationData.Current.ClearAsync(); //clear all files i.e. reset player.
                CreateDB(); //create the DB again.
            }
        }
question

Most helpful comment

Try to add ";connection=shared" to your connection string. It works for me.

All 4 comments

Hi @theweavrs, seams you are open datafile with an non-closed instance in memory. You are opening your datafile in exclusive mode, so you can't open another instance (file are in exclusive mode)

@mbdavid So what do you suggest?

If you want use exclusive mode you can always create a new instance of LiteDatabase. You need store your instance in a static variable, so you can re-using without open new.

Try to add ";connection=shared" to your connection string. It works for me.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MoamenMohamed picture MoamenMohamed  路  4Comments

josephinenewbie picture josephinenewbie  路  3Comments

axelgenus picture axelgenus  路  3Comments

kuiperzone picture kuiperzone  路  4Comments

sandolkakos picture sandolkakos  路  3Comments