webdad3
webdad3

Reputation: 9080

wp7: App failing! Can not figure out where?

I think I've narrowed it down to this code in the Deactivation event:

Here's the thing... When I put a break point in this code everything works fine. The application does NOT fail. However, when I take the break point off it fails. What I don't understand is why the try/catch isn't firing.

I should also note that I commented everything out of this event with no break point and the application worked fine. So it is something in this code...

Could it be that the Save Event is not finished for the unsaved object and when it tries to reactivate the Activation event is actually the one failing???

private void Application_Deactivated(object sender, DeactivatedEventArgs e)
{
    //MessageBox.Show("deactivated");
    try
    {
        //if ((Application.Current as App).infoSaved == false)
        //{
        unsaved unSavedPillInfo = new unsaved();
        unSavedPillInfo.RXName = (Application.Current as App).appRXName;
        unSavedPillInfo.RXNumber = (Application.Current as App).appRXNumber;
        unSavedPillInfo.DosageNotes = (Application.Current as App).appDosageNotes;
        unSavedPillInfo.Generic = (Application.Current as App).appGeneric;
        unSavedPillInfo.Instructions = (Application.Current as App).appInstructions;
        unSavedPillInfo.Reason = (Application.Current as App).appReason;

        unSavedPillInfo.Quantity = (Application.Current as App).appQuantity;
        unSavedPillInfo.Refills = (Application.Current as App).appRefills;

        unSavedPillInfo.Doctor = (Application.Current as App).appDoctor;
        unSavedPillInfo.DoctorNumber = (Application.Current as App).appDoctorNumber;
        unSavedPillInfo.Pharmacy = (Application.Current as App).appPharmacy;
        unSavedPillInfo.PharmacyNumber = (Application.Current as App).appPharmacyNumber;

        unSavedPillInfo.OrigDate = (Application.Current as App).appOrigDate;
        unSavedPillInfo.ReorderReminder = (Application.Current as App).appReorderReminder;
        unSavedPillInfo.ReorderDate = (Application.Current as App).appReorderDate;
        unSavedPillInfo.ConsumptionFrequency = (Application.Current as App).appConsumptionFrequency;

        unSavedPillInfo.PerscriptionUpdated = (Application.Current as App).perscriptionUpdated;
        unSavedPillInfo.PerscriptionUpdated = (Application.Current as App).doctorUpdated;
        unSavedPillInfo.PerscriptionUpdated = (Application.Current as App).detailsUpdated;
        unSavedPillInfo.PerscriptionUpdated = (Application.Current as App).pharmacyUpdated;

        unSavedPillInfo.Save();
        //}
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message.ToString());
    }

}

Upvotes: 2

Views: 408

Answers (2)

Edward
Edward

Reputation: 7424

This is hardly perfect but try putting a Messagebox inside of each of the eventhandlers. That way you can tell when each one is firing and see if one isn't firing.

Also you might want to unistall the application often to clear the IsolatedStorage. This is known to create issues if you keep running on the same installation.

EDIT: Yeah from what I have run into, the application can hang if you aren't properly saving to isolated storage. It can also happen if you aren't properly loading data from isolated storage. You might want to try each one seperately. Use a messagebox to make sure it saves and loads properly because VisualStudio will exit the current debugging session.

UPDATE What you should do is create a global variable called unsavedPrescription. Now when the user selects a prescription assign the global variable "unsaved" the prescription they selected. Note: you should not be assigning properties when the app is deactivating because it is very possible it won't save completely which leads to the app hanging. Instead all you have to do is assign the selected prescription to the global variable and change your code in App.xaml.cs to the following:

public unsaved unsavedPrescription {get; set;}

private void Application_Deactivated(object sender, DeactivatedEventArgs e)
{
    //Open up IsolatedStorageSettings
    IsolatedStorageSettings settings = Isolated StorageSettings.ApplicationSettings;

    //Use a model to save prescription
        //So create a name/value pair to store the prescription in isolatedstorage
    //Notice we used the global variable
    settings["UnsavedPrescription"] = unsavedPrescription;
}

private void Application_Activated(object sender, ActivatedEventArgs e)
{
    //Now you can easily load the prescription you saved
    //I'm reassigning the global variable that will contain the savedprescription

    if(settings.TryGetValue("UnsavedPrescription", out prescription)
    {
        unsavedPrescription = prescription;
    }
}

This greatly simplifies your code when loading and saving. Also you'll be able to test using the messageboxes like I said earlier, which isn't professional but it works nicely. Also your not running too much stuff when the app is trying to deactivate. THIS WAY WILL WORK AS I TESTED IT. The way you did it above looks like your running to much code while the app is deactivating which is probably why its hanging. Also this explains why when you remove it, everything works.

Upvotes: 2

William Melani
William Melani

Reputation: 4268

I had a problem that seems similar to yours that wasn't actually related to the Saving/Loading from IsolatedStorage itself, but rather the property I was setting / getting had inifinite recursion. This caused the application to terminate before getting to Catch statements.

It might be worthwhile for you to turn off the Debugger stepping over Properties: Tools -> Options -> Debugger -> Step over Properties and Operators (Managed Only)

public Dictionary<string, object> Dictionary
        {
            get
            {
                if (_dictionary == null)
                    _dictionary = new Dictionary<string, object>();
                return _dictionary;
            }
            set
            {
                Dictionary = value;
            }
        }

App Never launches successfully again after Tombstoning. No exception thrown

Upvotes: 0

Related Questions