locked
How Do I keep the Screen on while my application is running RRS feed

  • Question

  • Hi,

    I have an app that I am porting to HTC Snap. On I would like to keep the screen on while my application is running. I can of course change the phone settings to never turn the screen off. But unfortunately on this particular phone it is not possible to turn the screen off by pressing a button. At least I don't think there is: I have checked the phone's manual. As a result I cannot ask users to change their phone's settings to always keep the screen on because then they would have no way of turning off the screen short of turning off the device.

     

    So this is what I woulds like to do: I would like to change the phone's settings to keep the screen on when my application starts and put the settings back to its original state just before the application exits. I have looked at similar questions on the various forums but I could not find an answer to may question.

     

    By the way, I am using C# and the .NET Compact framework for this app. I would very much appreciate some help.

     

    Reza

    Sunday, May 9, 2010 8:03 AM

All replies

  • Use SystemIdleTimerResetSHIdleTimerReset with Timer (each 9 sec) to get the result.

    An alternative is to use SetSystemPowerState with POWER_FORCE flag and Timer but it can produce bad results (some devices will wake up on their own).


    If You'll find my answer satisfactory or helpful - mark it as answered or vote for it! Thank You.
    If You think You know better then me, why is Your code not working, then don't waste my time at this forum. Otherwise - do as I'm suggesting.

    I'm on MSDN just like MD House in the clinic. But I'm also a human which sometimes needs to see another doctor :)
    • Proposed as answer by Mal Loth Sunday, May 9, 2010 9:55 AM
    • Marked as answer by Guang-Ming Bian - MSFT Wednesday, May 12, 2010 6:52 AM
    • Unmarked as answer by Reza65 Monday, May 24, 2010 2:09 PM
    Sunday, May 9, 2010 9:55 AM
  • Use SystemIdleTimerReset.

    See the following page on how to efficiently call SystemIdleTimerReset API to prevent device from suspending.

    http://msdn.microsoft.com/en-us/library/bb158564.aspx


    - Onkar


    http://www.42gears.com
    SureRC : Remotely connect, view and control your Windows Mobile devices
    • Edited by Onkar [42Gears] Sunday, May 9, 2010 2:30 PM Added link for calling in c# code
    • Marked as answer by Guang-Ming Bian - MSFT Wednesday, May 12, 2010 6:51 AM
    • Unmarked as answer by Reza65 Monday, May 24, 2010 2:10 PM
    Sunday, May 9, 2010 2:26 PM
  • Thanks Mal, Onkar,

    First of all, I am sorry about the delay. For 2 weeks I was not able to check the answers. I do appreciate the help. It looks like these should be the answer. However, SystemIdleTimerReset dos not seem to work! Here is what I have:

     

    In the constructor of my main form I have:

                idleTimerResetter = new Thread(ResetIdleTime);
                idleTimerResetter.Start();

     

    The function RESETIdleTime is defined as follows:

              private void ResetIdleTime()
            {
                while (true)
                {
                    SystemIdleTimerReset();
                    Thread.Sleep(20000);
                }
            }

     

    Now, why doesn't this work? The device still turns off as before.

    Monday, May 24, 2010 2:22 PM
  • That's because You're using it in a thread. Use Timer control (the one from System.Forms) as I've recommended.


    If You'll find my answer satisfactory or helpful - mark it as answered or vote for it! Thank You.

    I'm on MSDN just like MD House in the clinic. But I'm also a human which sometimes needs to see another doctor :)

    After 3 years of WiMo developement - now switching to Bada OS.
    So long WM's lack of everything, welcome fully fledged Bada SDK :D.
    Monday, May 24, 2010 4:29 PM
  • Can you be more specific Mal?  This is what I did:

     

                timer = new System.Windows.Forms.Timer();
                timer.Tick += new EventHandler(ResetIdleTime);
                timer.Interval = 20000;
                timer.Enabled = true;   //no Start method on the .NET Compact platform

     

            private void ResetIdleTime(object sender, EventArgs args)
            {
                SystemIdleTimerReset();
            }

     

    It still doesn't seem to work. Also, I don't understand why calling SystemIdleTimerReset() would not work when called from my own thread.

     

    Reza

    Monday, May 24, 2010 6:54 PM
  • How do You test it? Normally You use should use the method and then call the timer. You can check this by setting control panel power off to 30 sec (lowest value) and Your device should not go to sleep.

    The metod given is a 100% working method and a general knowledge (everybody uses it successfuly on WM devices), therefore either: a) you're doing something wrong (p/invoke error perhaps) or b) your device's OEM implemented it wrong and You'll have to use SetSystemPowerState from coredll.dll as a last resort.

    In case of a) I'm showing how it should be done (works on all of my 3 devices):

    [DllImport("coredll.dll", EntryPoint = "SystemIdleTimerReset", SetLastError = true)]
    private extern static void SystemIdleTimerReset();
    
    [DllImport("aygshell.dll", EntryPoint = "SHIdleTimerReset", SetLastError = true)]
    private extern static void SHIdleTimerReset();
    
    // in designer
    
    private System.Windows.Forms.Timer suspentionTimer = new System.Windows.Forms.Timer();
    
    this.suspentionTimer.Interval = 29000;
    this.suspentionTimer.Tick += new System.EventHandler(this.bklTimer_Tick);
    
    // in form
    
    private void StartSuspendControl()
    {
    	// resetuje suspend i lock timery 
    	SystemIdleTimerReset();
    	SHIdleTimerReset();
    
    	suspentionTimer.Enabled = true;
    }
    
    private void StopSuspendControl()
    {
    	suspentionTimer.Enabled = false;
    			
    	SystemIdleTimerReset();
    	SHIdleTimerReset();
    
    	SetSystemPowerState(null, 0x00010000, 0);
    	// 0x02000000 = backlight on ; 0x00010000 = device on
    }
    
    private void bklTimer_Tick(object sender, EventArgs e)
    {
    	SystemIdleTimerReset();		// suspend prevention
    	SHIdleTimerReset();			// device lock prevention
    }
    
    public ProgressDialog()
    {
    	InitializeComponent();
    	StartSuspendControl();
    
    	// do something here for a long time without using Sleep() which suspends the timer allowing device to go to sleep
    
    	StopSuspendControl();
    }


    If You'll find my answer satisfactory or helpful - mark it as answered or vote for it! Thank You.

    I'm on MSDN just like MD House in the clinic. But I'm also a human which sometimes needs to see another doctor :)

    After 3 years of WiMo developement - now switching to Bada OS.
    So long WM's lack of everything, welcome fully fledged Bada SDK :D.
    Monday, May 24, 2010 9:37 PM
  • Ps. With this method backlight WILL turn off, but the device WON'T go to sleep. You want to retain the backlight only? If so then use just the SetSystemPowerState.
    If You'll find my answer satisfactory or helpful - mark it as answered or vote for it! Thank You.

    I'm on MSDN just like MD House in the clinic. But I'm also a human which sometimes needs to see another doctor :)

    After 3 years of WiMo developement - now switching to Bada OS.
    So long WM's lack of everything, welcome fully fledged Bada SDK :D.
    Monday, May 24, 2010 9:51 PM
  • Thanks for the detailed response Mal. Your code is essentially the same as what I had. I just spent most of the day (it is night time here) trying to figure out what is wrong. I double checked that SystemIdleTimerRest() gets called and it does. In fact, I tried the code on another device (HTC Touch2) and it works perfectly. But it doesn't work on the HTC Snap. This particular device behaves a little strange. The device turns off the screen after a period of inactivity. It also seems to deactivate the GPS driver (which I am using in my app to get GPS data) but it does not suspend my app which keeps running. However, without proper GPS data the app cannot do anything useful.

     

    Now this behavior does not change at all when I call SystemIdleTimerRest() perodically. The call seems to have no effect at all. This particular phone runs WM 6.1 Standard (Not professional), but I don't see why that would make a difference. Presumably, if the SystemIdleTimerReset function is available,  it would have some kind of effect.

     

    Reza

    Tuesday, May 25, 2010 6:57 PM
  • More references about GPS and device suspending - link.

    I'd try using SetSystemPowerState(NULL, POWER_STATE_ON, POWER_FORCE) where POWER_FORCE = 0x1000 and POWER_STATE_ON = 0x10000. This will keep the whole device awake, but You will need to be careful with that, because it may produce unforseen consequences (like device self waking up from suspention and/or discharging its batteries quickly). Therefore You should use the same method without POWER_FORCE flag, to unlock the power requirement.


    If You'll find my answer satisfactory or helpful - mark it as answered or vote for it! Thank You.

    I'm on MSDN just like MD House in the clinic. But I'm also a human which sometimes needs to see another doctor :)

    After 3 years of WiMo developement - now switching to Bada OS.
    So long WM's lack of everything, welcome fully fledged Bada SDK :D.
    Tuesday, May 25, 2010 8:25 PM
  • Thanks Mal. Looks like none of the power management routines work on this particular phone. This is especially annoying since the phone does not have a button to turn the screen off manually. That means if I set the phone to not turn the screen off after a period of inactivity, the screen will always be on! No way to turn the screen off unless I change the settings! I don't know what the manufacturer was thinking when they designed this phone, but if this is not incompetence, I don't know what is.

    I have not completely given up (yet) but right now I can't think of anything else to do. I could try and keep the GPS receiver on after the screed turns off. That would be better than doing nothing, but I really need the screen on as well. In the meantime if anyone can think of a solution I would appreciate it.

    Thanks again for the help.

    Thursday, May 27, 2010 8:42 PM
  • I had same probllems SystemIdleTimerReset() does not seem to work on newer devices

    I solved the problem see this thread

    http://social.msdn.microsoft.com/Forums/en-US/windowsmobiledev/thread/5246b4f6-05dc-4a5e-939f-9cb88f7819ae

    Friday, May 28, 2010 12:51 AM