Question

I have a problem, when I try to use this code:

using (IsolatedStorageFile myISF = IsolatedStorageFile.GetUserStoreForApplication())         
{
    try
    {
        object _readLock = new object();
        lock (_readLock)
        {
            var myFS = myISF.CreateFile(reply_FileName.Replace(" ", "_"));

            StreamWriter mySW = new StreamWriter(myFS);
            mySW.WriteLine(jsonToSave);
            mySW.Close();
        }
    }
    catch (IsolatedStorageException ex) { MessageBox.Show(ex.Message); }                
}

I have already try StreamWriter with using too, but the problem wasn't resolved. I have 2 page, on first page I use:

using (myFS = new IsolatedStorageFileStream(Forms_path + form_ID + ".json",
                  FileMode.Create, FileAccess.Write, myISF))
{
    using (StreamWriter mySW = new StreamWriter(myFS))
    {
        mySW.WriteLine(json);
        mySW.Close();
    }
    myFS.Dispose();
}
myFS.Close();

It's working good. Just the second code is faulty. I tried a lot of variations, but neither works good.

EDIT: when the problem occurred, I get this message in my Output window:

A first chance exception of type 'System.IO.IsolatedStorage.IsolatedStorageException' occurred in mscorlib.ni.dll

EDIT2: The problem was my filename. I added the date to my filename, the ":" and the "/" signs caused the trouble. I feel pathetic...

Was it helpful?

Solution 2

The problem was my filename. I added the date to my filename, the ":" and the "/" signs caused the trouble. I feel pathetic...

OTHER TIPS

You create the lock object every time the method called so it will not block other threads, thus the exception. The _readLock variable should be a static field in your class.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top