Jump to content

SetThreadExecutionState to keep screen awake


Xichael
 Share

Recommended Posts

While 1
   Sleep(50)
Opt("WinTitleMatchMode")
If WinExists("Netflix") Then
   DllCall("Kernel32.dll","int","SetThreadExecutionState","int",0x80000000,"int",0x00000002)
   WinWaitClose("Netflix")
EndIf
WEnd

Netflix isn't keeping the screen awake while watching videos, but fortunately there's SetThreadExecutionState. It says I'll need both ES_CONTINUOUS and ES_DISPLAY_REQUIRED for it to work. Am I correct that in the DllCall those would have to be 0x80000000 and 0x00000002, respectively?

What am I doing wrong here? This script it crashing AutoIt entirely, with no error message whatsoever.

[Edited Title]

Edited by Xichael
Link to comment
Share on other sites

It's probably crashing because of that extra imaginary parameter you added. Read the docs again.

Try something like

"int", 0x80000000 + 0x00000002

(not tested)

Edited by AdmiralAlkex
Link to comment
Share on other sites

It's probably crashing because of that extra imaginary parameter you added. Read the docs again.

Try something like

"int", 0x80000000 + 0x00000002

(not tested)

Thanks. It's not crashing now.

I was wondering how to put the two together. I couldn't find anything in the docs about it. So, it's a "+" to put two parameters in one?

Did you start Netflix within the same process that is issueing setthreadexecutionstate?

No. How would I do that? I'm opening Netflix in Chrome, which has a process per tab and then some. How do you have a process issue a dll? Is it necessary for my purposes?

As you may gather, I'm new to all of this, and programming in general, so I'll take all the guidance I can get.

Edited by Xichael
Link to comment
Share on other sites

Xichael,

If your problem is that the sreen is being powered off, change the power scheme. I'm not sure that this function has anything to do with keeping your screen on, however, I'm new to Windows programming also so maybe an expert will weigh in.

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

change the power scheme

That's how I was originally doing it, but I wanted it to work with any power scheme without needing any tweaking to correctly reset the sceme after Netflix is closed. Here's what I was using:
Local $Ran
While 1
   Sleep(50)
  
If WinExists("Netflix - Google Chrome") and $Ran=0 Then
   Run("powercfg -change -monitor-timeout-ac 0")
   $Ran=1
ElseIf WinWaitClose("Netflix - Google Chrome") and $Ran=1 Then
   Run("powercfg -change -monitor-timeout-ac 5")
   $Ran=0
EndIf
WEnd

Then I read about SetThreadExecutionState, and it seems like a better way to do it, if I can get it working properly.

This works well, but it's perpetually doing the DllCall, over and over. I'm just worried that it's bad for some reason; like it's wearing out poor Kernel32:

While 1
   Sleep(50)
Opt("WinTitleMatchMode")
If WinExists("Netflix") Then
   DllCall("Kernel32.dll","int","SetThreadExecutionState","int",0x00000002)
EndIf
WEnd

There must be good reasons I shouldn't do it this way, right?

Edited by Xichael
Link to comment
Share on other sites

Ok, just reading up on it. It's called an initializer? It says it's required, but I haven't used it before. I guess when it's not declared, it defaults to 0?

Almost. With a simple script test we can prove that it's an empty string. Like if you had used $vTest = ""

Local $vTest
ConsoleWrite(VarGetType($vTest) & @LF)
ConsoleWrite(StringLen($vTest) & @LF)

And that worked? I don't see how the 1st if is ever true.. IF you are using $Ran as a toggle variable than declare it like

local $Ran = 0

Well, yes.

ConsoleWrite(("" = 0) & @LF)

But I would still set it to = 0 to look consistent and avoid the behind the scenes convert.

Link to comment
Share on other sites

Thanks. It's not crashing now.

I was wondering how to put the two together. I couldn't find anything in the docs about it. So, it's a "+" to put two parameters in one?

There is only one parameter!

EXECUTION_STATE WINAPI SetThreadExecutionState(

__in EXECUTION_STATE esFlags

);

Count that. One. That's all.

As for the flags, if you want for example ES_SYSTEM_REQUIRED (1) and ES_DISPLAY_REQUIRED (2), then yeah, you add* them together (1+2). How else would you get to 3?

*Well, the article tells you to use BitOr() ("This parameter can be one or more of the following values."), for the thinking behind that, see Setting Styles in the wiki. But the world will not end if you just add them in this case as the numbers are unique.

Link to comment
Share on other sites

Flags. That's the term I was looking for. Still new to all the nomenclature. What's the "es" in esFlags? And they seem to be referring to the same thing as Styles in the article. Are Flags Styles?

The article also mentions that "+" and BitOR will return different results. Looks like BitOR is the way to go.

I appreciate all the insight, Admiral. Thanks.

Link to comment
Share on other sites

I imagine that this would take care of the endless calls to run:

Global Const $program_name = "Netflix - Google Chrome"

While True
    If WinExists($program_name) Then
        Run("powercfg -change -monitor-timeout-ac 0")

        While True
            If Not WinExists($program_name) Then
                Run("powercfg -change -monitor-timeout-ac 5")
                ExitLoop
            EndIf

            Sleep(50)
        WEnd
    EndIf

    Sleep(50)
WEnd

Edited by LaCastiglione
Link to comment
Share on other sites

LaCastiglione

I imagine that this would take care of the endless calls to run...

I was actually referring to the second script in the post. That first one was working fine; just not the best approach. Thanks for showing me another way of doing things though.

That BitOR is doing it's job quite nicely keeping the screen awake. However, after Netflix closes, it's not turning off again. The documentation says:

ES_CONTINUOUS 0x80000000 Informs the system that the state being set should remain in effect until the next call that uses ES_CONTINUOUS and one of the other state flags is cleared.

So I suppose I need to clear the flags? How would I do that?
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...