Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

What code can I use to prevent the screen to turn off while my application is running on Phone ?

Posted on 2009-07-10
12
Medium Priority
?
362 Views
Last Modified: 2013-12-27
I have written a program in Visual Studio 2008 for a Smart Device (Windows Based).
Program is in .Net
What I want is that the screen light doesn't turn off at all while the application is running.
I suppose it is caused by the energy settings of my Mobile Device, but I want to overrule these settings.

F.E. Just like TomTom manages to do on my Smartphone.

I could really use your help here, because there's little to find about this subject.
0
Comment
Question by:karstieman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
12 Comments
 
LVL 24

Expert Comment

by:alexey_gusev
ID: 24826951
look at SetPowerRequirement() and power management in general, some example is available here:

http://social.msdn.microsoft.com/Forums/en-US/vssmartdevicesnative/thread/37ecff14-0415-4c9f-a404-e4a98078bd0e
0
 
LVL 48

Expert Comment

by:Mikal613
ID: 24827920
0
 
LVL 5

Author Comment

by:karstieman
ID: 24831607
Thanks for the help guys !

I now have the code ( class ) and can manage the backlight to stay on.

But I want the option to give the backlight control back to the mobile device too.
Here I still have a problem, because after enabling the backlight programmatically and turning it off (what doens't work well untill now), whenever the screen turns completely off, it turns on again and off, and on, etc.

The class I use is in the code snippet.

This I use to make the backlight stay on: SetPowerRequirement("BKL1:", PowerState.D0, 1, IntPtr.Zero, 0)

This is what I use to turn it off, and what doens't work completely.
ReleasePowerRequirement(Handle)


SO PLEASE HELP IN TURNING THE BACKLIGHT OFF AGAIN AND GIVE BACKLIGHT CONTROL BACK TO THE MOBILE DEVICE
Public Class Backlight
    Public Declare Function SetPowerRequirement Lib "coredll.dll" (ByVal pvDevice As String, ByVal DeviceState As PowerState, ByVal DeviceFlags _
As Integer, ByVal pvSystemState As IntPtr, ByVal StateFlags As Integer) As IntPtr
    Public Declare Function ReleasePowerRequirement Lib "coredll.dll" (ByVal handle As IntPtr) As Integer
    'ensure the power requirement is released
    Protected Overrides Sub Finalize()
        Release()
    End Sub
    'handle to the power requirement
    Public handle As IntPtr
    Public Enum PowerState
        PwrDeviceUnspecified = -1
        D0 = 0 'full on _
        D1 = 1 'low power
        D2 = 2 'standby
        D3 = 3 'sleep
        D4 = 4 'off
        PwrDeviceMaximum = 5
    End Enum
    'keep the backlight lit
    Public Sub Activate()
        'request full power
        handle = SetPowerRequirement("BKL1:", PowerState.D0, 1, IntPtr.Zero, 0)
    End Sub
    'keep the backlight lit
    Public Sub DeActivate()
        'request no power
        handle = SetPowerRequirement("BKL1:", PowerState.D4, 1, IntPtr.Zero, 0)
    End Sub
    Public Function PowerSettingChanged() As Boolean
        If handle = IntPtr.Zero Then
            Return False
        Else
            Return True
        End If
    End Function
    'release power requirement
    Public Sub Release()
        If handle.ToInt32() <> 0 Then
            Dim result As Integer
            result = ReleasePowerRequirement(handle)
            handle = IntPtr.Zero
        End If
    End Sub
 
 
 
End Class

Open in new window

0
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 
LVL 24

Accepted Solution

by:
alexey_gusev earned 1500 total points
ID: 24831887
this SetPowerRequirement()  function was always kind of trouble :)

try attached class, I guess we use the same original code, but with few modifications here and there
Backlight.txt
0
 
LVL 5

Author Comment

by:karstieman
ID: 24833510
@Alexey Gusev:Thanks for the code !

But I'm still not able to give backlight control back to the pda...
Using the code "ReleasePowerRequirement(Handle)" nothing happens...

What line do you use to exactly for this ?
0
 
LVL 24

Expert Comment

by:alexey_gusev
ID: 24833520
quite possible that you would also need to SendMessage(HWND_BROADCAST,WM_SETTIGNSCHANGE,0xF2,0), but usually SetSystemPowerState() worked.
0
 
LVL 5

Author Comment

by:karstieman
ID: 24838783
@Alexey Gusev: I have used your class and coed to keep backlight on works fine !
LIGHT ON --> "Backlight.SetPowerRequirement("BKL1:", DevicePowerState.D0, 1, IntPtr.Zero, 0)"

But I need the exact Vb code to turn backlight OFF again, so the Mobile Device gains control again.

I now have put it under a button for testing purposes, but it doens't work :(

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        DeviceApplication1.Backlight.ReleasePowerRequirement(Handle)
End Sub

What do you use for this ?
0
 
LVL 24

Expert Comment

by:alexey_gusev
ID: 24838845
I use

SetSystemPowerState(sysPowerStateName, flags, POWER_FORCE)

in addition to ReleasePowerRequirement(Handle) [look at Release method]
besides, try to call SendMessage() with parameters from my prev post. It all might need just proper combination of power management functions and gettting the OS to refresh its data
0
 
LVL 5

Author Comment

by:karstieman
ID: 24839034
When I use
SetSystemPowerState(sysPowerStateName, flags, POWER_FORCE)
from my application it says:

'DeviceApplication1.Backlight.Private Shared Function SetSystemPowerState(psState As String, StateFlags As Integer, Options As Integer) As Integer' is not accessible in this context because it is 'Private'.      
0
 
LVL 24

Expert Comment

by:alexey_gusev
ID: 24839064
well, make it public
0
 
LVL 5

Author Comment

by:karstieman
ID: 24842354
It's better if I make a new queston about this.
You helped me with me initial question, so thank you very much !
0
 
LVL 5

Author Closing Comment

by:karstieman
ID: 31602255
Thank you for your assistance !
0

Featured Post

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we will discuss all things related to StageFright bug, the most vulnerable bug of android devices.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question