4

隔離されたストレージをミューテックスで保護して、モバイル アプリケーションとBackgroundAudioPlayer.

これらは、isostorage 内のファイルにアクセスするためのヘルパー クラスです。

    public static async Task WriteToFile(string text)
    {
        using (var mut = new Mutex(false, "IsoStorageMutex"))
        {
            mut.WaitOne();
            try
            {
                // Get the text data from the textbox. 
                byte[] fileBytes = System.Text.Encoding.UTF8.GetBytes(text.ToCharArray());

                // Get the local folder.
                var local = ApplicationData.Current.LocalFolder;

                // Create a new folder name DataFolder.
                var dataFolder = await local.CreateFolderAsync("MusicFolder",
                                                               CreationCollisionOption.OpenIfExists);

                // Create a new file named DataFile.txt.
                var file = await dataFolder.CreateFileAsync("Streams.txt",
                                                            CreationCollisionOption.ReplaceExisting);

                // Write the data from the textbox.
                using (var s = await file.OpenStreamForWriteAsync())
                {
                    s.Write(fileBytes, 0, fileBytes.Length);
                }
            }
            finally
            {
                mut.ReleaseMutex();
            }
        }
    }

    public static async Task<string> ReadFile()
    {
        using (var mut = new Mutex(false, "IsoStorageMutex"))
        {
              mut.WaitOne();
              var result = String.Empty;
              try
              {
                  // Get the local folder.
                  var local = ApplicationData.Current.LocalFolder;

                  if (local != null)
                  {
                      // Get the DataFolder folder.
                      var dataFolder = await local.GetFolderAsync("MusicFolder");

                      // Get the file.
                      var file = await dataFolder.OpenStreamForReadAsync("Streams.txt");

                      // Read the data.

                      using (var streamReader = new StreamReader(file))
                      {
                          result = streamReader.ReadToEnd();
                      }
                  }
              }
              finally
              {
                  mut.ReleaseMutex();
              }
              return result;
        }
    }

しかし、バックグラウンド エージェントでアクセスしようとすると、次のエラーが発生します。

 Object synchronization method was called from an unsynchronized block of code.

スタックトレース:

   at System.Threading.Mutex.ReleaseMutex()
   at YouRadio.IsolatedStorage.StorageHelpers.<ReadFile>d__b.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at YouRadio.AudioPlaybackAgent.AudioPlayer.<AddTracksFromIsoStorageToPlaylist>d__6.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
   at YouRadio.AudioPlaybackAgent.AudioPlayer.<OnUserAction>d__2.MoveNext()

私が間違っていることは何ですか?

4

1 に答える 1