Search code examples
windows-phone-8sensorsapplication-lifecycle

WP8 app resuming with sensors


In my application on WP8 I work with all of the sensors. Accelerometer, Compass (from Windows.Devices.Sensors and from Microsoft.Devices.Sensors too) and Inclinometer. Everything works fine until I push the Windows button and after that the Back button. I get a full black screen with a "Resuming..." message and nothing happens. Once I got an exception (from 5 fauilure only once):

{System.Runtime.InteropServices.SEHException: External component has thrown an exception.
   at Microsoft.Devices.Sensors.SensorBase`1.PauseSensor()
   at Microsoft.Devices.Sensors.SensorBase`1.<>c__DisplayClass7.<.ctor>b__4(Object sender, NotificationCallbackEventArgs args)
   at Microsoft.Devices.Sensors.SensorCallback.MS.Internal.Interop.INotificationCallback.Pausing(XPauseType pauseType)
   at MS.Internal.Interop.NotificationService.NotificationPausing(XPauseType pauseType)
   at MS.Internal.FrameworkCallbacks.NotificationPausing(UInt32 pauseType)}

Did someone meet with this exception?

Or is there a best practice working correctly with sensors during the wp8 application lifecycle?

Thanks


Solution

  • This problem was more complex than I thought. I’ve worked a lot with this. I share what I found because I think my experiences with it could help later someone else too.

    I worked with the sensors through service classes: these classes update my VMs and in the VMs I use my DispatcherHelper to update these on the UI. I knew if I want to update UI bound properties in valuechanged events of the sensors I need to call this update in the eventhandler with a dispatcherhelper but what I did not know is how they behave if they are communicating with VMs instead of Views. When I started the app I had no problems with updating the UI if the sensors had new value. My only problem was in this case that if I wanted to deactivate (pressed windows button) and activate (pressed back button) my app, I had no exception, I did not lost my debug session but only got a black page with a „Resuming…” text and running progressbar and nothing happened.

    What I missed was a crossthread/accessing exception. If I do it with a UI bound data it throws an exception but in this case it did not.

    One other thing: the accelerometer (I had a version only working with the accelerometer) works with the emulator but on the device it does not. I think the emulator try to simulate the accelerator by giving back values, but not in a right way (without a threading issue).

    Solution

    If I update within an eventhandler on the UI thread (with my own DispatcherHelper) everything works fine. It is a good lesson learned, always pay attention when interacting with sensors and other layers/threads (VM,V).