ThreadSafe IsoStorageManager This is a continuation of the last thread.

I wanted to add a thread-safety and I was considering AwaitableCriticalSection and Task+Mutex.  I ended up with mutex; it can be a bit slower but it would allow different files to be stored independently.

public static async Task&lt;T&gt; ReadJson&lt;T&gt;(String fileName)
{
    if (String.IsNullOrEmpty(fileName)) return default(T);

    return await TaskEx.Run(() =&gt;
    {
        using (var myMutex = new Mutex(false, String.Format("{0}@{1}", AppUid, fileName)))
        {
            try
            {
                myMutex.WaitOne();
                using (var store = IsolatedStorageFile.GetUserStoreForApplication())
                using (var stream = new IsolatedStorageFileStream(fileName, FileMode.Open, store))
                using (var sr = new StreamReader(stream))
                using (var jr = new JsonTextReader(sr))
                    return IsoStorageEx.jsonSerializer.Deserialize&lt;T&gt;(jr);
            }
            finally 
            { 
                myMutex.ReleaseMutex(); 
            }
        }
    });
}


public static async Task&lt;bool&gt; WriteJson&lt;T&gt;(String fileName, T content)
{
    if (String.IsNullOrEmpty(fileName))
        return false;

    return await TaskEx.Run(() =&gt;
        {
            using (var myMutex = new Mutex(false, String.Format("{0}@{1}", AppUid, fileName)))
            {
                try
                {
                    myMutex.WaitOne();
                    using (var store = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        store.CreateFolderIfNecessary(fileName);
                        using (var stream = new IsolatedStorageFileStream(fileName, FileMode.Create, FileAccess.Write, store))
                        using (var sw = new StreamWriter(stream))
                        using (var jw = new JsonTextWriter(sw))
                            IsoStorageEx.jsonSerializer.Serialize(jw, content);
                    }
                }
                finally 
                { 
                    myMutex.ReleaseMutex(); 
                }

                return true;
            }
        });
}


Where AppUid is just a random string, so mutexes of different apps would not collide.

Sample can be downloaded here.

Any suggestions?
public static async Task&lt;T&gt; ReadJson&lt;T&gt;(String fileName)


The Task-based asynchronous pattern says that the names of Task-returning methods should end with Async, e.g. ReadJsonAsync.



if (String.IsNullOrEmpty(fileName)) return default(T);


Why return the default value here? Empty fileName seems like a bug that should be discovered as soon as possible, so a more reasonable (and common) response would be to throw an exception.



TaskEx.Run


Usually, calling Task.Run shouldn't be done in a library, it should be done by the user. That way, the user will understand that the method is not truly asynchronous. But this library seems to be specifically meant to be called from the UI thread, so I guess it's okay here.



I don't quite understand the need for the Mutex here. Do you really expect to access the same file multiple times at the same time from one application? That doesn't sound like a common situation to me.