Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…re running multiple instances cause various exceptions to be thrown.
  • Loading branch information
JimiC committed Oct 22, 2015
1 parent 3580185 commit eca36e6
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 32 deletions.
8 changes: 4 additions & 4 deletions Source/CSharpAnalytics/AutoMeasurement/BaseAutoMeasurement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,20 +202,20 @@ protected async Task StartRequesterAsync()
/// Suspend the requester and preserve any unsent URIs.
/// </summary>
/// <returns>Task that completes when the requester has been suspended.</returns>
protected async Task StopRequesterAsync()
protected void StopRequesterAsync()
{
var safeBackgroundRequester = backgroundRequester;
if (safeBackgroundRequester == null) return;

var recentRequestsToPersist = new List<Uri>();
if (safeBackgroundRequester.IsStarted)
{
var pendingRequests = await safeBackgroundRequester.StopAsync();
var pendingRequests = safeBackgroundRequester.StopAsync().GetAwaiter().GetResult();
recentRequestsToPersist = pendingRequests.Skip(pendingRequests.Count - MaximumRequestsToPersist).ToList();
}

await Save(recentRequestsToPersist, QueueStorageName);
await Save(sessionManager.GetState(), SessionStorageName);
Save(recentRequestsToPersist, QueueStorageName);
Save(sessionManager.GetState(), SessionStorageName);

safeBackgroundRequester.Dispose();
backgroundRequester = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ protected override bool IsInternetAvailable()
/// </summary>
/// <param name="sender"></param>
/// <param name="eventArgs"></param>
private async void ApplicationOnExit(object sender, EventArgs eventArgs)
private void ApplicationOnExit(object sender, EventArgs eventArgs)
{
UnhookEvents();
await StopRequesterAsync();
StopRequesterAsync();
}
}
}
4 changes: 2 additions & 2 deletions Source/CSharpAnalytics/AutoMeasurement/WpfAutoMeasurement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ protected override bool IsInternetAvailable()
/// </summary>
/// <param name="sender"></param>
/// <param name="eventArgs"></param>
private async void ApplicationOnExit(object sender, EventArgs eventArgs)
private void ApplicationOnExit(object sender, EventArgs eventArgs)
{
UnhookEvents();
await StopRequesterAsync();
StopRequesterAsync();
}
}
}
48 changes: 24 additions & 24 deletions Source/CSharpAnalytics/Serializers/AppDataContractSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Reflection;
using System.Runtime.Serialization;
using System.Threading.Tasks;
using System.Xml;

namespace CSharpAnalytics.Serializers
{
Expand Down Expand Up @@ -48,36 +49,32 @@ private static string GetDefaultFolderPath()
public static async Task<T> Restore<T>(string filename = null, bool deleteBadData = false)
{
var serializer = new DataContractSerializer(typeof(T), new[] { typeof(DateTimeOffset) });
var settings = new XmlReaderSettings { Async = true };

try
{
var file = GetFilePath<T>(filename);

try
{
using (var inputStream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, true))
using (var fileStream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, true))
using (var xmlReader = XmlReader.Create(fileStream, settings))
{
if (inputStream.Length == 0)
{
return default(T);
}

using (var memoryStream = new MemoryStream())
{
await inputStream.CopyToAsync(memoryStream);
await inputStream.FlushAsync();
memoryStream.Seek(0, SeekOrigin.Begin);

return (T)serializer.ReadObject(memoryStream);
}
await xmlReader.ReadAsync();
return (T)serializer.ReadObject(xmlReader);
}
}
catch (SerializationException)
{
if (deleteBadData)
File.Delete(file);
throw;
}
catch (XmlException)
{
if (deleteBadData)
File.Delete(file);
}

return default(T);
}
catch (FileNotFoundException)
{
Expand All @@ -95,20 +92,23 @@ public static async Task<T> Restore<T>(string filename = null, bool deleteBadDat
public static async Task Save<T>(T value, string filename = null)
{
var serializer = new DataContractSerializer(typeof(T), new[] { typeof(DateTimeOffset) });

var settings = new XmlWriterSettings { Indent = true, Async = true };
var file = GetFilePath<T>(filename);

using (var memoryStream = new MemoryStream())
try
{
serializer.WriteObject(memoryStream, value);

using (var fileStream = new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.None, 4096, true))
using (var xmlWriter = XmlWriter.Create(fileStream, settings))
{
memoryStream.Seek(0, SeekOrigin.Begin);
await memoryStream.CopyToAsync(fileStream);
await fileStream.FlushAsync();
serializer.WriteObject(xmlWriter, value);
await xmlWriter.FlushAsync();
}
}
catch (UnauthorizedAccessException)
{
System.Diagnostics.Debug.WriteLine(
"Failed to save to {0}. You may have insufficient rights or a synchronization may be occuring.", file);
}
}

/// <summary>
Expand All @@ -125,4 +125,4 @@ private static string GetFilePath<T>(string filename)
return Path.Combine(FolderPath, filename ?? typeof(T).Name);
}
}
}
}

0 comments on commit eca36e6

Please sign in to comment.