From 504cc8bb50ed9ca23339dbdf1f00b49423d9c36d Mon Sep 17 00:00:00 2001 From: Christian Fiedler Date: Wed, 14 Aug 2013 14:32:02 +0200 Subject: [PATCH] onFailedSave_WriteThread --- CampusAppWP8/CampusAppWP8/Model/MainModel.cs | 26 ++++++++- CampusAppWP8/CampusAppWP8/Utility/File.cs | 59 +++++++++++++++++--- 2 files changed, 76 insertions(+), 9 deletions(-) diff --git a/CampusAppWP8/CampusAppWP8/Model/MainModel.cs b/CampusAppWP8/CampusAppWP8/Model/MainModel.cs index ba793fda..ef64f4e2 100644 --- a/CampusAppWP8/CampusAppWP8/Model/MainModel.cs +++ b/CampusAppWP8/CampusAppWP8/Model/MainModel.cs @@ -172,6 +172,11 @@ namespace CampusAppWP8 /// public event OnFailed onFailed = null; + /// + /// Callback pointer, called after failed saving data to file. + /// + public event OnFailed onFailedSave = null; + /// /// Callback pointer, for checking if file is up to date at loading. /// @@ -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(); } + */ } } diff --git a/CampusAppWP8/CampusAppWP8/Utility/File.cs b/CampusAppWP8/CampusAppWP8/Utility/File.cs index ae66a2e5..f03c9be8 100644 --- a/CampusAppWP8/CampusAppWP8/Utility/File.cs +++ b/CampusAppWP8/CampusAppWP8/Utility/File.cs @@ -9,6 +9,7 @@ namespace CampusAppWP8.Utility { using System; using System.IO; + using System.Threading; using Windows.Storage; /// @@ -96,6 +97,11 @@ namespace CampusAppWP8.Utility ReadOnly = 3 } + /// + /// Delegation of the write callback function prototype. + /// + public delegate void WriteCallbackFunc(); + /// /// Read data from file to a string. /// @@ -133,24 +139,30 @@ namespace CampusAppWP8.Utility /// /// data byte array /// write type - 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); } /// @@ -218,14 +230,15 @@ namespace CampusAppWP8.Utility /// Write data asynchronous to file. /// /// data array - 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(); + } } } } \ No newline at end of file