1

In my program, I'm trying to use a List of employees and mark them as fired (this is just for fun, of course). I can make the program do anything that I want except remember what I've already done. So, I'm trying to save the list in a file, then read that file back into the program when the program starts. Unfortunately, when the program is forced to close, the same function that is supposed to read the file causes the program to crash. I don't know that I can debug the program since I'm performing a task, closing the program (which closes the debugger) and then opening the program again...

Here's my function that is awaited when a "start" button is clicked.

async private Task readJsonAsync()
{
    var jsonSerializer = new DataContractSerializer(typeof(List<Employee>));

    try
    {
        var myStream = await ApplicationDate.Current.LocalFolder.OpenStreamForReadAsync(JSONFILENAME);
        employees = (List<Employee>)jsonSerializer.ReadObject(myStream);
    }
    catch (FileNotFoundException ex)
    {
        employees = Employee.Initialize();
    }
}

When I save the List, it seems to work. When I reopen the app after force closing on the emulator, I get "The program '[3156] employeeTest05.exe' has exited with code -1073741819 (0xc0000005) 'Access violation'" before I can even see the "start" button. When I reopen the app on a device after force closing, I can get into the app, but I can't start using the program because it crashes without an error on the screen, and the debugger is closed.

Am I going about this the wrong way? I care how the properties of the Employee objects get saved, only that they are saved and can be retrieved.

jasotastic
  • 396
  • 1
  • 2
  • 16

1 Answers1

0

The case you want is called Tomestoning. You can read the best practices here: App activation and deactivation for Windows Phone 8.

private void Application_Deactivated(object sender, DeactivatedEventArgs e)
{
// TODO: call your save function
// PhoneApplicationService.Current.State["your_key"] = your_value;
}

And if you want a generic read and write example. Please refer to my answer on saving/loading a list as an xml document here. You can modify it to use your JSON.

Generic Read and Write XML

Community
  • 1
  • 1
Chubosaurus Software
  • 8,133
  • 2
  • 20
  • 26