0
\$\begingroup\$

I have difficulties to implement loading a savegame file in my Windows Phone project. I use EasyStorage to load and save files. I get the following exception when I try to load after saving a file.

An exception of type 'System.IO.IsolatedStorage.IsolatedStorageException' occurred in mscorlib.ni.dll but was not handled in user code

I'm sure that I get this exception because saving is not finished when I try to load the file. But how can I solve the problem? I want to load the file after it was created. The file needs to be created when you start the game the first time.

What is the best way to solve the problem?

public void LoadingData() 
    { 
        if (saveDevice.IsReady == true) 
        { 
            if ((saveDevice.FileExists("TestContainer", "Data" + ".txt")) == false) 
            { 
                // save a file asynchronously. this will trigger IsBusy to return true 
                // for the duration of the save process. 
                saveDevice.SaveAsync( 
                  "TestContainer", 
                  "Data" + ".txt", 
                  stream => 
                  { 
                      using (StreamWriter writer = new StreamWriter(stream)) 
                      { 
                          writer.WriteLine(10 + ";" + 2 + ";" + 2); 
                          int Start = -50; 
                          int Offset = 150; 
                          int Row = 150; 
                          for (int i = 0; i <= 5 - 1; i++) 
                          { 
                              Start += Offset; 
                              writer.WriteLine("Image" + (i + 1).ToString() + ";" + Start + ";" + Row + ";" + false); 
                          } 
                      } 
                  }); 

                saveDevice.Load( 
            "TestContainer", 
             "Data" + ".txt", 
            stream => 
            { 
                using (StreamReader reader = new StreamReader(stream)) 
                { 
                    strs = reader.ReadLine().Split(';'); 
                    game1.Levels = int.Parse(strs[0]); 
                    game1.LevelProgress = int.Parse(strs[1]); 
                    game1.CurrentLevel = int.Parse(strs[2]); 
                    strs = reader.ReadLine().Split(';'); 
                    game1.levellisttexstring.Add(strs[0]); 
                    game1.levellistvec.Add(new Vector2(int.Parse(strs[1]), int.Parse(strs[2]))); 
                    game1.levellist.Add(bool.Parse(strs[3])); 
                    strs = reader.ReadLine().Split(';'); 
                    game1.levellisttexstring.Add(strs[0]); 
                    game1.levellistvec.Add(new Vector2(int.Parse(strs[1]), int.Parse(strs[2]))); 
                    game1.levellist.Add(bool.Parse(strs[3])); 
                    strs = reader.ReadLine().Split(';'); 
                    game1.levellisttexstring.Add(strs[0]); 
                    game1.levellistvec.Add(new Vector2(int.Parse(strs[1]), int.Parse(strs[2]))); 
                    game1.levellist.Add(bool.Parse(strs[3])); 
                    strs = reader.ReadLine().Split(';'); 
                    game1.levellisttexstring.Add(strs[0]); 
                    game1.levellistvec.Add(new Vector2(int.Parse(strs[1]), int.Parse(strs[2]))); 
                    game1.levellist.Add(bool.Parse(strs[3])); 
                    strs = reader.ReadLine().Split(';'); 
                    game1.levellisttexstring.Add(strs[0]); 
                    game1.levellistvec.Add(new Vector2(int.Parse(strs[1]), int.Parse(strs[2]))); 
                    game1.levellist.Add(bool.Parse(strs[3])); 
                } 
            });            
            } 
            else 
            { 
                saveDevice.Load( 
                "TestContainer", 
                 "Data" + ".txt", 
                stream => 
                { 
                    using (StreamReader reader = new StreamReader(stream)) 
                    { 
                        strs = reader.ReadLine().Split(';'); 
                        game1.Levels = int.Parse(strs[0]); 
                        game1.LevelProgress = int.Parse(strs[1]); 
                        game1.CurrentLevel = int.Parse(strs[2]); 
                        strs = reader.ReadLine().Split(';'); 
                        game1.levellisttexstring.Add(strs[0]); 
                        game1.levellistvec.Add(new Vector2(int.Parse(strs[1]), int.Parse(strs[2]))); 
                        game1.levellist.Add(bool.Parse(strs[3])); 
                        strs = reader.ReadLine().Split(';'); 
                        game1.levellisttexstring.Add(strs[0]); 
                        game1.levellistvec.Add(new Vector2(int.Parse(strs[1]), int.Parse(strs[2]))); 
                        game1.levellist.Add(bool.Parse(strs[3])); 
                        strs = reader.ReadLine().Split(';'); 
                        game1.levellisttexstring.Add(strs[0]); 
                        game1.levellistvec.Add(new Vector2(int.Parse(strs[1]), int.Parse(strs[2]))); 
                        game1.levellist.Add(bool.Parse(strs[3])); 
                        strs = reader.ReadLine().Split(';'); 
                        game1.levellisttexstring.Add(strs[0]); 
                        game1.levellistvec.Add(new Vector2(int.Parse(strs[1]), int.Parse(strs[2]))); 
                        game1.levellist.Add(bool.Parse(strs[3])); 
                        strs = reader.ReadLine().Split(';'); 
                        game1.levellisttexstring.Add(strs[0]); 
                        game1.levellistvec.Add(new Vector2(int.Parse(strs[1]), int.Parse(strs[2]))); 
                        game1.levellist.Add(bool.Parse(strs[3])); 
                    } 
                }); 
            } 
        } 
    }
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Notice how you used saveAsync. Either listen for an event from saveAsync to tell you it's done, or use sycronous saving, then load when the call finishes

\$\endgroup\$
3
  • \$\begingroup\$ saveDevice.Save works. But I don't understand the difference between saveDevice.Save and saveDevice.SaveAsync. What is the difference? Is saveDevice.Save a good way to solve this problem? \$\endgroup\$
    – Bryan
    Commented Jul 6, 2013 at 16:18
  • \$\begingroup\$ The difference is that save async starts saving, then whilst it is saving, it allows the calling code to continue to run. Save runs synchronously, that is, the calling code waits for the save to finish before it continues. The advantage to async is that your game can do other things during the save, ie. show a saving screen, etc. \$\endgroup\$
    – Outurnate
    Commented Jul 6, 2013 at 18:32
  • \$\begingroup\$ Thanx. I need no saving screen, so I will use saveDevice.Save. \$\endgroup\$
    – Bryan
    Commented Jul 6, 2013 at 19:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .