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

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.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

karstiemanAuthor Commented:
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.

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()
    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
            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

Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
karstiemanAuthor Commented:
@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 ?
quite possible that you would also need to SendMessage(HWND_BROADCAST,WM_SETTIGNSCHANGE,0xF2,0), but usually SetSystemPowerState() worked.
karstiemanAuthor Commented:
@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
End Sub

What do you use for this ?
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
karstiemanAuthor Commented:
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'.      
well, make it public
karstiemanAuthor Commented:
It's better if I make a new queston about this.
You helped me with me initial question, so thank you very much !
karstiemanAuthor Commented:
Thank you for your assistance !
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Smartphone Programming

From novice to tech pro — start learning today.