Skip to content

Commit c4e0836

Browse files
committed
Minor file lock improvements.
1 parent 336da9b commit c4e0836

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

ReflectXMLDB/DatabaseHandler.cs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class DatabaseHandler : DatabaseHandlerBase
1818
private FileSystemWatcher fileSystemWatcher = null;
1919
private IEnumerable<string> paths = null;
2020
private string lastFileChanged = string.Empty;
21-
private static readonly object _object = new object();
21+
private static readonly object lockObject = new object();
2222

2323
#endregion
2424

@@ -238,7 +238,14 @@ private T GetDatabase<T>() where T : IDatabase
238238

239239
if (!string.IsNullOrEmpty(path))
240240
{
241-
return path.Deserialize<T>();
241+
T database = default(T);
242+
243+
lock(lockObject)
244+
{
245+
database = path.Deserialize<T>();
246+
}
247+
248+
return database;
242249
}
243250
else
244251
{
@@ -276,8 +283,6 @@ private ICollection<T> Get<T>(string propertyName = null, object propertyValue =
276283
}
277284
else
278285
{
279-
//var _items = dbCollection.Where(item => item.GetType().GetProperty(propertyName).GetValue(item).ToString() == propertyValue.ToString());
280-
281286
List<T> col = new List<T>();
282287
foreach (var item in dbCollection)
283288
{
@@ -348,7 +353,7 @@ public void CreateDatabase<T>() where T : IDatabase
348353
((IDatabase)db).GUID = GetNextGUID<T>();
349354
var xml = db.Serialize(true);
350355

351-
lock(_object)
356+
lock(lockObject)
352357
{
353358
xml.Save(path);
354359
}
@@ -364,7 +369,7 @@ public void DeleteDatabase<T>() where T : IDatabase
364369

365370
if (File.Exists(path))
366371
{
367-
lock(_object)
372+
lock(lockObject)
368373
{
369374
File.Delete(path);
370375
}
@@ -475,7 +480,7 @@ public void Save<T>(ICollection<T> items) where T : ICollectableObject
475480

476481
string path = GetDatabasePath(dbInfo.DatabaseType);
477482

478-
lock (_object)
483+
lock (lockObject)
479484
{
480485
db.Serialize(true).Save(path);
481486
}
@@ -569,7 +574,7 @@ public void ImportDatabase(string fileToImport, string exportPath)
569574
//the path
570575
string fileUnzipFullName = string.Empty;
571576

572-
lock(_object)
577+
lock(lockObject)
573578
{
574579
//Opens the zip file up to be read
575580
using (ZipArchive archive = ZipFile.OpenRead(fileToImport))
@@ -619,7 +624,7 @@ public void ExportDatabase(string pathToSave, string filename, string fileExtens
619624

620625
string fullFilePath = Path.Combine(pathToSave, filename + fileExtension);
621626

622-
lock(_object)
627+
lock(lockObject)
623628
{
624629
ZipFile.CreateFromDirectory(CurrentWorkspace, fullFilePath);
625630
}

0 commit comments

Comments
 (0)