Skip to content

Instantly share code, notes, and snippets.

@mikoskinen
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikoskinen/9203782 to your computer and use it in GitHub Desktop.
Save mikoskinen/9203782 to your computer and use it in GitHub Desktop.
Windows Phone storage, testing thread safety with Isolated Storage.
The code which writes a file using Isolated Storage:
private void CreateNewFileUsingIsolatedStorage(string filePath)
{
using (var myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
using (var fileStream = myIsolatedStorage.OpenFile(filePath, FileMode.Create, FileAccess.Write))
{
using (var writer = new StreamWriter(fileStream))
{
var someTextData = "Some content";
writer.WriteLine(someTextData);
}
}
}
}
Simulated test:
for (int i = 0; i < 3; i++)
{
ThreadPool.QueueUserWorkItem(x =>
{
var fileName = "File_" + i;
CreateNewFileUsingIsolatedStorage(fileName);
});
}
Problem: Isolated Storage randomly throws this exception:
System.IO.IsolatedStorage.IsolatedStorageException was unhandled
HResult=-2146233264
Message=Operation not permitted on IsolatedStorageFileStream.
Source=mscorlib
StackTrace:
at System.IO.IsolatedStorage.IsolatedStorageFileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, IsolatedStorageFile isf)
at System.IO.IsolatedStorage.IsolatedStorageFile.OpenFile(String path, FileMode mode, FileAccess access)
at PhoneApp95.MainPage.CreateNewFile(String filePath)
at PhoneApp95.MainPage.<>c__DisplayClass2.<ButtonBase_OnClick>b__0(Object x)
at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()
InnerException:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment