IdleDetectionMode Error Windows 7 Phone

I have a checkbox. On this checked event, I want to disable IdleDetectionMode and on the unverified event that I want to turn on. This is the code: -

private void chkRunInBackground_Checked(object sender, RoutedEventArgs e) { PhoneApplicationService.Current.ApplicationIdleDetectionMode = IdleDetectionMode.Disabled; } private void chkRunInBackground_Unchecked(object sender, RoutedEventArgs e) { PhoneApplicationService.Current.ApplicationIdleDetectionMode = IdleDetectionMode.Enabled; } 

The checked event works fine, but with the unchecked event that I receive, the IdleDetection mode cannot be fired after it is disabled. Why does this restriction apply and what can I do to get around it?

+4
source share
2 answers

From MSDN :

In the current version of the application, the definition of downtime cannot be included in one instance of the application when it was disabled. This makes an exception. This may be supported in future releases, so your application may choose to turn off application idle detection when it is no longer needed and catch the expected exception.

The following code fragment shows an implementation of this.

 // Custom function to turn off idle detection. This will throw an exception in the current release. public void TryReenableApplicationIdleDetection() { bool didEnable = false; try { Microsoft.Phone.Shell.PhoneApplicationService.Current.ApplicationIdleDetectionMode = Microsoft.Phone.Shell.IdleDetectionMode.Enabled; didEnable = true; } catch (InvalidOperationException ex) { // This exception is expected in the current release. } // possibly use the value of didEnable to decide what to do next // if it is 'true' then your app will be deactivated // if it is 'false' then your app will keep running } 
+4
source

This is by design. By MSDN :

In the current version of the application, the definition of downtime cannot be included in one instance of the application when it was disabled. This makes an exception.

Basically, the application determines its characteristics, which will determine the behavior of the system and the "attitude" to it. It’s bad practice to try to change them while the application is running.

+2
source

All Articles