Skip to content

Commit

Permalink
Minor file lock improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
Fe-Bell committed Mar 30, 2018
1 parent 336da9b commit c4e0836
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions ReflectXMLDB/DatabaseHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class DatabaseHandler : DatabaseHandlerBase
private FileSystemWatcher fileSystemWatcher = null;
private IEnumerable<string> paths = null;
private string lastFileChanged = string.Empty;
private static readonly object _object = new object();
private static readonly object lockObject = new object();

#endregion

Expand Down Expand Up @@ -238,7 +238,14 @@ private void StopMonitoringDirectory()

if (!string.IsNullOrEmpty(path))
{
return path.Deserialize<T>();
T database = default(T);

lock(lockObject)
{
database = path.Deserialize<T>();
}

return database;
}
else
{
Expand Down Expand Up @@ -276,8 +283,6 @@ private void StopMonitoringDirectory()
}
else
{
//var _items = dbCollection.Where(item => item.GetType().GetProperty(propertyName).GetValue(item).ToString() == propertyValue.ToString());

List<T> col = new List<T>();
foreach (var item in dbCollection)
{
Expand Down Expand Up @@ -348,7 +353,7 @@ public void ClearHandler()
((IDatabase)db).GUID = GetNextGUID<T>();
var xml = db.Serialize(true);

lock(_object)
lock(lockObject)
{
xml.Save(path);
}
Expand All @@ -364,7 +369,7 @@ public void ClearHandler()

if (File.Exists(path))
{
lock(_object)
lock(lockObject)
{
File.Delete(path);
}
Expand Down Expand Up @@ -475,7 +480,7 @@ public void ClearHandler()

string path = GetDatabasePath(dbInfo.DatabaseType);

lock (_object)
lock (lockObject)
{
db.Serialize(true).Save(path);
}
Expand Down Expand Up @@ -569,7 +574,7 @@ public void ImportDatabase(string fileToImport, string exportPath)
//the path
string fileUnzipFullName = string.Empty;

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

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

lock(_object)
lock(lockObject)
{
ZipFile.CreateFromDirectory(CurrentWorkspace, fullFilePath);
}
Expand Down

0 comments on commit c4e0836

Please sign in to comment.