Add activate statistics

This commit is contained in:
qianlifeng
2015-01-23 21:52:46 +08:00
parent 42d86fab8e
commit 4379145231
15 changed files with 83 additions and 56 deletions

View File

@@ -6,6 +6,7 @@ using System.Reflection;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading;
using Wox.Infrastructure.Logger;
namespace Wox.Infrastructure.Storage
@@ -18,6 +19,7 @@ namespace Wox.Infrastructure.Storage
[Serializable]
public abstract class BinaryStorage<T> : BaseStorage<T> where T : class, IStorage, new()
{
private static object syncObject = new object();
protected override string FileSuffix
{
get { return ".dat"; }
@@ -87,25 +89,31 @@ namespace Wox.Infrastructure.Storage
protected override void SaveInternal()
{
try
ThreadPool.QueueUserWorkItem(o =>
{
FileStream fileStream = new FileStream(ConfigPath, FileMode.Create);
BinaryFormatter binaryFormatter = new BinaryFormatter
lock (syncObject)
{
AssemblyFormat = FormatterAssemblyStyle.Simple
};
binaryFormatter.Serialize(fileStream, serializedObject);
fileStream.Close();
}
catch (Exception e)
{
Log.Error(e.Message);
try
{
FileStream fileStream = new FileStream(ConfigPath, FileMode.Create);
BinaryFormatter binaryFormatter = new BinaryFormatter
{
AssemblyFormat = FormatterAssemblyStyle.Simple
};
binaryFormatter.Serialize(fileStream, serializedObject);
fileStream.Close();
}
catch (Exception e)
{
Log.Error(e);
#if (DEBUG)
{
throw;
}
{
throw;
}
#endif
}
}
}
});
}
}
}

View File

@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using Newtonsoft.Json;
namespace Wox.Infrastructure.Storage
@@ -12,6 +13,7 @@ namespace Wox.Infrastructure.Storage
/// </summary>
public abstract class JsonStrorage<T> : BaseStorage<T> where T : class, IStorage, new()
{
private static object syncObject = new object();
protected override string FileSuffix
{
get { return ".json"; }
@@ -39,8 +41,14 @@ namespace Wox.Infrastructure.Storage
protected override void SaveInternal()
{
string json = JsonConvert.SerializeObject(serializedObject, Formatting.Indented);
File.WriteAllText(ConfigPath, json);
ThreadPool.QueueUserWorkItem(o =>
{
lock (syncObject)
{
string json = JsonConvert.SerializeObject(serializedObject, Formatting.Indented);
File.WriteAllText(ConfigPath, json);
}
});
}
}
}