Page 1 of 2

Keyboard input hangs after approx 30 minutes

Posted: Tue Jan 30, 2018 3:09 am
by bytecave
Hi again, all. I noticed that a game I'm working on causes keyboard input to stop if the PC isn't touched for about 1/2 hour. I experimented and boiled the code down to this. Just press '1' to make the random number change, then walk away for 1/2 hour. When you come back, see if things still works when you press the '1' key. Nothing for me.

Keyboard stops working on my main desktop (W8.1), my laptop (W10), and my PC at work (W10) too. My actual game loop isn't this tight, plenty of graphics and logic are happening each pass. Even so, perhaps I'm calling ExamineKeyboard() too much or something? I tried this with 5.61, 5.62beta1 and 5.62beta2, all 64-bit on both AMD and Intel CPUs. In debug and release modes, within IDE and as a standalone compiled EXE. Can anyone repro this? Any ideas what's going on? Thanks again, in advance. :)

Code: Select all

Define Event.i, Text.s = "00000"

If InitSprite() = 0 Or InitKeyboard() = 0
  MessageRequester("PB Hang Test", "Init failed.")
  End
EndIf

OpenWindow(0, 0, 0, 800, 600, "Test App", #PB_Window_SizeGadget | #PB_Window_SizeGadget | #PB_Window_MaximizeGadget | #PB_Window_Maximize)
OpenWindowedScreen(WindowID(0), 0, 0, 1024, 768, #True, 0, 0, #PB_Screen_SmartSynchronization)

While #True
  Repeat
    Event = WindowEvent()
    
    If Event = #PB_Event_CloseWindow
      End
    EndIf
  Until Event = 0
  
  StartDrawing(ScreenOutput())
    BackColor($FFFFFF)
    FrontColor($000000)
    DrawText(100, 100, Text)
  StopDrawing()
  
  FlipBuffers()
  
  ExamineKeyboard()
  If KeyboardReleased(#PB_Key_1)
    Text = RSet(Str(Random(10000)), 5, "0")
  EndIf
Wend

Re: Keyboard input hangs after approx 30 minutes

Posted: Tue Jan 30, 2018 4:07 am
by citystate
I'd imagine it has something to do with the lack of Delay() in your main loop; your code would be taking most of your system's processing, which could cause problems if another process attempts to run

Re: Keyboard input hangs after approx 30 minutes

Posted: Tue Jan 30, 2018 4:14 am
by bytecave
Great, I will add a Delay and give it a shot!

Re: Keyboard input hangs after approx 30 minutes

Posted: Tue Jan 30, 2018 7:05 am
by bytecave
I added Delay(10) at the end right before the Wend statement. Unfortunately, the result is unchanged and the keyboard still stops working after a time. I apologize for not knowing, but is there a better place to put the Delay() statement and yield to the OS? Thank you.

Re: Keyboard input hangs after approx 30 minutes

Posted: Tue Jan 30, 2018 7:45 am
by nco2k
the whole delay thing is total nonsense. this sounds more like some sort of energy saving issue.

c ya,
nco2k

Re: Keyboard input hangs after approx 30 minutes

Posted: Tue Jan 30, 2018 12:19 pm
by #NULL
Maybe try with IsScreenActive(), but i don't really know if that could help :)
BTW here on linux your code works even after suspend-to-ram.

Code: Select all

...
  If IsScreenActive()
    StartDrawing(ScreenOutput())
      BackColor($FFFFFF)
      FrontColor($000000)
      DrawText(100, 100, Text)
    StopDrawing()
   
    FlipBuffers()
   
    ExamineKeyboard()
    If KeyboardReleased(#PB_Key_1)
      Text = RSet(Str(Random(10000)), 5, "0")
    EndIf
  EndIf

Re: Keyboard input hangs after approx 30 minutes

Posted: Tue Jan 30, 2018 9:39 pm
by kpeters58
Looks like a Windows sleep issue to me. By default, Sleep is set to 30 minutes for Windows 10 (and quite likely for Win 7 & 8 as well, though I haven't checked for lack of such boxes).

To check if that is your problem, set your Sleep interval to a minute or two and see if that's it.

If that turns out to be the issue, then the question becomes whether it's just your app having problems waking the system or other apps as well. Check that and then report back here.

Re: Keyboard input hangs after approx 30 minutes

Posted: Tue Jan 30, 2018 10:23 pm
by infratec
Hm...

I had a problem that my 'slow' upload was interrupted by windows sleep.
So I wrote a small tool to keep it awake.

Code: Select all

EnableExplicit

#POWER_REQUEST_CONTEXT_VERSION = 0
#POWER_REQUEST_CONTEXT_SIMPLE_STRING = 1
#POWER_REQUEST_CONTEXT_DETAILED_STRING = 2

Enumeration PowerRequestType
  #PowerRequestDisplayRequired
  #PowerRequestSystemRequired
  #PowerRequestAwayModeRequired
  CompilerIf #PB_Compiler_OS > #PB_OS_Windows_7 Or #PB_Compiler_OS > #PB_OS_Windows_Server_2008_R2
    #PowerRequestExecutionRequired
  CompilerEndIf
EndEnumeration


Structure DetailedStructure
  LocalizedReasonModule.l
  LocalizedReasonId.l
  ReasonStringCount.l
  *ReasonStrings
EndStructure


Structure REASON_CONTEXT_Structure
  Version.l
  Flags.l
  StructureUnion
    Detailed.DetailedStructure
    *SimpleReasonString
  EndStructureUnion
EndStructure


Prototype.i Prototype_PowerCreateRequest(*REASON_CONTEXT.REASON_CONTEXT_Structure)
Prototype.i Prototype_PowerSetRequest(PowerRequest.i, RequestType.i)
Prototype.i Prototype_PowerClearRequest(PowerRequest.i, RequestType.i)

Global PowerCreateRequest.Prototype_PowerCreateRequest
Global PowerSetRequest.Prototype_PowerSetRequest
Global PowerClearRequest.Prototype_PowerClearRequest

Define.i hndl, lib
Define ReasonContext.REASON_CONTEXT_Structure



lib = OpenLibrary(#PB_Any, "Kernel32.dll")
If lib
  PowerCreateRequest = GetFunction(lib, "PowerCreateRequest")
  PowerSetRequest = GetFunction(lib, "PowerSetRequest")
  PowerClearRequest = GetFunction(lib, "PowerClearRequest")
  CloseLibrary(lib)
 
  ReasonContext\Version = #POWER_REQUEST_CONTEXT_VERSION
  ReasonContext\Flags = #POWER_REQUEST_CONTEXT_SIMPLE_STRING
  ReasonContext\SimpleReasonString = @"Test"
 
  hndl = PowerCreateRequest(@ReasonContext)
  If hndl <> #INVALID_HANDLE_VALUE
    If PowerSetRequest(hndl, #PowerRequestAwayModeRequired)
      If PowerSetRequest(hndl, #PowerRequestSystemRequired)
        
        MessageRequester("No windows standby", "when work finished, press OK")
        
        PowerClearRequest(hndl, #PowerRequestSystemRequired)
      EndIf
      PowerClearRequest(hndl, #PowerRequestAwayModeRequired)
    EndIf
    CloseHandle_(hndl)
  EndIf
EndIf
Try if your code can run longer as 30 minutes when this code is running.

Bernd

Re: Keyboard input hangs after approx 30 minutes

Posted: Tue Jan 30, 2018 11:49 pm
by citystate
nco2k wrote:the whole delay thing is total nonsense. this sounds more like some sort of energy saving issue.

c ya,
nco2k
in my defense, as my sig states, I may be talking out of my hat :?

Re: Keyboard input hangs after approx 30 minutes

Posted: Tue Jan 30, 2018 11:50 pm
by bytecave
Thanks for the sleep notes. All of my PCs except the laptop are set to Never Sleep, with bubbles screen saver on after 1 hour. I remote into them from work or other client sites multiple times daily. I put this on a couple file servers at a client's site that definitely never sleep, same issue. Also tried on another PC at Microsoft. This repros reliability on every Windows machine--seven in total running either Win 8.1 or Win 10 with Never Sleep enabled--I've tried it on. I will do some reading and file this as a Windows bug. If anybody has gotten this to run for 1/2 hour on Windows, I'd be interested to know. Thanks again --Rich

Re: Keyboard input hangs after approx 30 minutes

Posted: Wed Jan 31, 2018 12:17 am
by JHPJHP
Hi bytecave,

Try the following: SetThreadExecutionState function (before the main loop)
- #ES_DISPLAY_REQUIRED stops the Screen-Saver from activating

Code: Select all

Prototype protoSetThreadExecutionState(esFlags.l)
Global SetThreadExecutionState.protoSetThreadExecutionState

kernel32 = OpenLibrary(#PB_Any, "kernel32.dll")
                
If IsLibrary(kernel32)
  SetThreadExecutionState = GetFunction(kernel32, "SetThreadExecutionState")
  SetThreadExecutionState(#ES_SYSTEM_REQUIRED | #ES_DISPLAY_REQUIRED | #ES_AWAYMODE_REQUIRED | #ES_CONTINUOUS)
  CloseLibrary(kernel32)
EndIf
If the laptop lid is closed while the program is running, the only way I found to keep it working was to call OpenWindowedScreen again.
- add the following code to test

Code: Select all

If KeyboardReleased(#PB_Key_2)
  CloseScreen()
  OpenWindowedScreen(WindowID(0), 0, 0, 1024, 768, #True, 0, 0, #PB_Screen_SmartSynchronization)
EndIf
NOTE: Even though the screen is not updated the numbers are still redrawn. This can be tested by pressing the 1 key while the screen seems frozen, before pressing the 2 key.

Re: Keyboard input hangs after approx 30 minutes

Posted: Wed Jan 31, 2018 10:49 am
by marc_256
Hi bytecave,
can you test this one please ?
Marc

Code: Select all

Define Event.i, Text.s = "00000"

If InitSprite() = 0 Or InitKeyboard() = 0
	MessageRequester("PB Hang Test", "Init failed.")
	End
EndIf

OpenWindow(0, 0, 0, 800, 600, "Test App", #PB_Window_SizeGadget | #PB_Window_SizeGadget | #PB_Window_MaximizeGadget | #PB_Window_Maximize)
OpenWindowedScreen(WindowID(0), 0, 0, 1024, 768, #True, 0, 0, #PB_Screen_SmartSynchronization)

	Repeat
		Event = WindowEvent()

		If Event = #PB_Event_CloseWindow
			Goto EndProg
		Else
			Delay(1)
		EndIf

		ExamineKeyboard()
			If KeyboardReleased(#PB_Key_1)
				Text = RSet(Str(Random(10000)), 5, "0")
			EndIf

		StartDrawing(ScreenOutput())
			BackColor($FFFFFF)
			FrontColor($000000)
			DrawText(100, 100, Text)

		StopDrawing()

		FlipBuffers()

		Delay(1)

	Until KeyboardPushed(#PB_Key_Escape)

EndProg:
	End

Re: Keyboard input hangs after approx 30 minutes

Posted: Wed Jan 31, 2018 7:37 pm
by bytecave
Thank you for the suggestions #NULL and marc_256. I tried with your changes, but still get a reliable keyboard hang on 7 out of 7 machines after 15-30 minutes. I can still size and drag the window, still just the keyboard input that stops.

I tried code to keep it awake and prevent screen saver too with no difference. It sounds like nobody has been able to get this to work for 30 minutes on Windows, just on Linux. Apologies if I missed it if you did.

Thanks everybody for the great suggestions and for taking a look!

Re: Keyboard input hangs after approx 30 minutes

Posted: Wed Jan 31, 2018 9:34 pm
by JHPJHP
Hi bytecave,

Did you see my previous post :?:

I tested the script on two separate machines, for over 30 minutes, without it "hanging". In addition, a work-around was provided for sleep mode (laptop lid closed then reopened).
bytecave wrote:... still just the keyboard input that stops.
During my tests the keyboard input still works, changing the numbers, but the screen was not refreshed.
- see my previous post for additional information

Re: Keyboard input hangs after approx 30 minutes

Posted: Wed Jan 31, 2018 10:20 pm
by Dude
I would try changing

Code: Select all

Event = WindowEvent()
to

Code: Select all

Event = WaitWindowEvent(1)
so that your main loop doesn't hold the CPU so tight.

Try it and let me know.