title: In WP7, data is not saved immediately after writing to IsolatedStorage
body: <p>I encountered this problem while polishing my WP7 application. </p>
  
  <p>Though I follow Microsoft's guidelines to store game state when it's being deactivated, I'd also like to save some data in runtime. </p>
  
  <p>The reason for this is that when the battery is removed from device, no deactivation / closing callbacks are triggered. </p>
  
  <p>The problem with this comes when the user walks through the game and accidentally removes the battery from her device - all game progress is lost.</p>
  
  <p>That's why I do save game state at some intermediate checkpoints, but I have noticed that data is not stored immediately. This is my "save" function:</p>
  
  <pre><code>    public void SaveAppModelToIsolatedStorage()
      {
          using (var store = IsolatedStorageFile.GetUserStoreForApplication())
          using (var stream = new IsolatedStorageFileStream(APPMODEL_DATAFILE,
                                                            FileMode.Create,
                                                            FileAccess.Write,
                                                            store))
          {
              var serializer = new XmlSerializer(typeof(AppModel));
  
              try
              {
                  serializer.Serialize(stream, AppModel);
              }
              catch (Exception ex)
              {
                  Debug.WriteLine("Cant serialize AppModel:" + ex.Message);
              }
          }
      }
  </code></pre>
  
  <p>After it's been called, if I remove the battery within a number of seconds (not sure how many but always less than 30), the application ends up with lost game progress. If I wait before removing battery, the data would be saved successfully. This behavior is observed on various WP7 phones.</p>
  
  <p>I also tried serialization into a string buffer and then writing that string buffer to the file by calling <code>stream.Write()</code>, but the result is the same. Also, <code>stream.Flush()</code> doesn't seem to have an effect.</p>
  
  <p>Is this behavior a platform feature? </p>
  
  <p>Is it fine in terms of Microsoft certification for Marketplace apps? </p>
  
  <p>Or is there a way to save data immediately?</p>