Skip to content

Commit

Permalink
added file lock check to BaseEntity
Browse files Browse the repository at this point in the history
  • Loading branch information
juwilliams committed Aug 9, 2017
1 parent 5b43eba commit 838e39a
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion SpatialConnect.Entity/BaseEntity.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Newtonsoft.Json;
using System.IO;

using System.Threading;

namespace SpatialConnect.Entity
{
public abstract class BaseEntity
Expand All @@ -14,7 +15,37 @@ public void Write(string path)
{
string json = JsonConvert.SerializeObject(this);

while (IsFileOpen(path))
{
Thread.Sleep(1000);
}

File.WriteAllText(path, json);
}

public bool IsFileOpen(string path)
{
FileStream stream = null;

try
{
stream = File.OpenRead(path);
}
catch (IOException ex)
{
_log.Debug("file locked, will retry (File: " + path + ")");

return true;
}
finally
{
if (stream != null)
{
stream.Close();
}
}

return false;
}
}
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified SpatialConnect.Windows.Task/bin/Release/gbc.dll
Binary file not shown.

0 comments on commit 838e39a

Please sign in to comment.