Merge branch 'hotfix/onFailedSave_WriteThread' into develop
This commit is contained in:
@@ -172,6 +172,11 @@ namespace CampusAppWP8
|
||||
/// </summary>
|
||||
public event OnFailed onFailed = null;
|
||||
|
||||
/// <summary>
|
||||
/// Callback pointer, called after failed saving data to file.
|
||||
/// </summary>
|
||||
public event OnFailed onFailedSave = null;
|
||||
|
||||
/// <summary>
|
||||
/// Callback pointer, for checking if file is up to date at loading.
|
||||
/// </summary>
|
||||
@@ -359,13 +364,30 @@ namespace CampusAppWP8
|
||||
}
|
||||
|
||||
byte[] data = this.SerializeModel();
|
||||
string test = Encoding.UTF8.GetString(data, 0, data.Length);
|
||||
this.file.WriteFile(data);
|
||||
|
||||
if ((this.onSaved != null) && (this.onFailedSave != null))
|
||||
{
|
||||
this.file.WriteFile(data, delegate() { this.onSaved(); }, delegate() { this.onFailedSave(); });
|
||||
}
|
||||
else if (this.onSaved != null)
|
||||
{
|
||||
this.file.WriteFile(data, delegate() { this.onSaved(); }, null);
|
||||
}
|
||||
else if (this.onFailedSave != null)
|
||||
{
|
||||
this.file.WriteFile(data, null, delegate() { this.onFailedSave(); });
|
||||
}
|
||||
else
|
||||
{
|
||||
this.file.WriteFile(data, null, null);
|
||||
}
|
||||
|
||||
/*
|
||||
if (this.onSaved != null)
|
||||
{
|
||||
this.onSaved();
|
||||
}
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ namespace CampusAppWP8.Utility
|
||||
{
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using Windows.Storage;
|
||||
|
||||
/// <summary>
|
||||
@@ -96,6 +97,11 @@ namespace CampusAppWP8.Utility
|
||||
ReadOnly = 3
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Delegation of the write callback function prototype.
|
||||
/// </summary>
|
||||
public delegate void WriteCallbackFunc();
|
||||
|
||||
/// <summary>
|
||||
/// Read data from file to a string.
|
||||
/// </summary>
|
||||
@@ -133,24 +139,30 @@ namespace CampusAppWP8.Utility
|
||||
/// </summary>
|
||||
/// <param name="data">data byte array</param>
|
||||
/// <param name="ioType">write type</param>
|
||||
public void WriteFile(byte[] data, IOTypeWrite ioType = IOTypeWrite.INVALID)
|
||||
public void WriteFile(byte[] data, WriteCallbackFunc onSavedCallback, WriteCallbackFunc onFailedCallback, IOTypeWrite ioType = IOTypeWrite.INVALID)
|
||||
{
|
||||
IOTypeWrite tempType = ioType;
|
||||
|
||||
|
||||
if (tempType == IOTypeWrite.INVALID)
|
||||
{
|
||||
tempType = this.writeType;
|
||||
}
|
||||
|
||||
/*
|
||||
if (tempType == IOTypeWrite.WriteAsync)
|
||||
{
|
||||
this.WriteAsync(data);
|
||||
this.WriteAsync(data, onSavedCallback, onFailedCallback);
|
||||
}
|
||||
else if (tempType == IOTypeWrite.WriteSync)
|
||||
{
|
||||
// this.WriteSync(data);
|
||||
this.WriteAsync(data);
|
||||
this.WriteAsync(data, onSavedCallback, onFailedCallback);
|
||||
}
|
||||
*/
|
||||
|
||||
Thread th = new Thread(delegate() { this.WriteAsync(data, onSavedCallback, onFailedCallback); });
|
||||
th.Start();
|
||||
//this.WriteAsync(data, onSavedCallback, onFailedCallback);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -218,14 +230,15 @@ namespace CampusAppWP8.Utility
|
||||
/// Write data asynchronous to file.
|
||||
/// </summary>
|
||||
/// <param name="data">data array</param>
|
||||
private async void WriteAsync(byte[] data)
|
||||
private async void WriteAsync(byte[] data, WriteCallbackFunc onSavedCallback, WriteCallbackFunc onFailedCallback)
|
||||
{
|
||||
Logger.LogMsg("writeasync file: " + this.filename);
|
||||
StorageFile file = null;
|
||||
Stream s = null;
|
||||
int counter = 0;
|
||||
|
||||
// try opening the file
|
||||
while (file == null)
|
||||
while ((file == null) && (counter < 10))
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -233,12 +246,23 @@ namespace CampusAppWP8.Utility
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
counter++;
|
||||
System.Threading.Thread.Sleep(10);
|
||||
}
|
||||
}
|
||||
|
||||
if (file == null)
|
||||
{
|
||||
if (onFailedCallback != null)
|
||||
{
|
||||
onFailedCallback();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
counter = 0;
|
||||
// try to get a stream on the file
|
||||
while (s == null)
|
||||
while ((s == null) && (counter < 10))
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -246,10 +270,20 @@ namespace CampusAppWP8.Utility
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
counter++;
|
||||
System.Threading.Thread.Sleep(10);
|
||||
}
|
||||
}
|
||||
|
||||
if (s == null)
|
||||
{
|
||||
if (onFailedCallback != null)
|
||||
{
|
||||
onFailedCallback();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// writing data
|
||||
try
|
||||
{
|
||||
@@ -258,10 +292,21 @@ namespace CampusAppWP8.Utility
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
if (onFailedCallback != null)
|
||||
{
|
||||
onFailedCallback();
|
||||
}
|
||||
Logger.LogException(e);
|
||||
s.Dispose();
|
||||
return;
|
||||
}
|
||||
|
||||
s.Dispose();
|
||||
|
||||
if (onSavedCallback != null)
|
||||
{
|
||||
onSavedCallback();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user