WP8 resumes working with sensors

In my WP8 application, I work with all sensors. Accelerometer, compass (from Windows.Devices.Sensors and from Microsoft.Devices.Sensors too) and an inclinometer. Everything works fine until I press the Windows button and then the Back button. I get a full black screen with the message "Resume ..." and nothing happens. As soon as I got an exception (from 5 feilors 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)} 

Has anyone encountered this exception?

Or is the correct operation with sensors effective during the life cycle of a wp8 application?

thanks

+4
source share
2 answers

This problem was more complicated than I thought. I have worked a lot with this. I share what I found, because I think that my experience with him can help someone else later.

I worked with sensors through service classes: these classes update my virtual machines, and in virtual machines I use my DispatcherHelper to update them in the user interface. I knew that if I wanted to update the associated properties of the interface in events with changed sensor values, I needed to call this update in the event handler using the dispatcher, but I did not know how they behave if they interact with virtual machines, and not with representations. When I started the application, I had no problems updating the user interface if the sensors had a new meaning. My only problem was that if I wanted to deactivate (press the window button) and activate (press the back button) my application, I had no exception, I did not lose the debugging session, but received only a black page with "resume". .. "and the progressbar progress, and nothing happened.

What I missed was the crossthread / accessing exception. If I do this with data bound to the user interface, it throws an exception, but in this case it is not.

One more thing: the accelerometer (I had a version that works only with the accelerometer) works with the emulator, but this is not the case on the device. I think the emulator is trying to simulate an accelerator, returning values, but not in the right direction (without a problem with threads).

Decision

If I update in the event handler in the UI thread (using my own DispatcherHelper), everything works fine. This is a good lesson that should always be considered when interacting with sensors and other layers / flows (VM, V).

+3
source

It sounds like a life cycle problem.

Did you try to call SensorBase.Stop () on the sensors from Microsoft.Devices.Sensors on the App.Deactivated event and resume using SensorBase.Start () in the App.Activated event? Depending on how many pages in your application need access to the sensor, you can even override the OnNavigatedTo / OnNavigatedFrom methods and use them.

You don't have to do this, but depending on your application, this might be a good idea. (Does your application work in lock screen mode in standby mode, does it have background agents? Is it trying to display sensor readings while working in the background?)

+1
source

All Articles