Jump to content

Problem with TimerDiff


Recommended Posts

Hi!

I'm trying to adapt piece of code found here  to set time limit for user to enter something in command prompt. 

I wrote something like this:

Local $tBuffer = DllStructCreate("char"), $nRead, $sRet = ""
Local $hFile = _WinAPI_CreateFile("CON", 2, 2)

$start = TimerInit()

While TimerDiff($start) < 10000
  _WinAPI_ReadFile($hFile, DllStructGetPtr($tBuffer), 1, $nRead)
  If DllStructGetData($tBuffer, 1) = @CR Then ExitLoop
  If $nRead > 0 Then $sRet &= DllStructGetData($tBuffer, 1)
WEnd

But it doesn't work. Command prompt waits endlessly for input.

 

I've tried changing "TimerDiff($start) < 10000 "  to "if TimerDiff($start) > 10000 Then ExitLoop " but it seems like condition is not even evaluated.

Am I missing something?

Link to comment
Share on other sites

  • Moderators

You need to do some debugging, to see where it is failing (i.e. does it recognize the TimerDiff or is it failing somewhere else). Try modifying your While statement and include a msgbox or ConsoleWrite, like this:

$start = TimerInit()

While 1
    If TimerDiff($start) > 10000 Then MsgBox(0, TimerDiff($start), "Time's Up!")
    Sleep(500)
WEnd

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

I've tried.

If I remove _WinAPI_ReadFile related code timer works, but with this three lines even if I insert some ConsoleWrite or FileWrite (eg. ConsoleWrite(Int(TimerDiff($start))) ) in this loop it looks like this piece of code is omitted. 

I don't know exactly what's going on in these three lines so maybe the problem is it's working completely different than I think it is. :)

Link to comment
Share on other sites

No, and I think it's not the point. It looks like condition is not evaluated.

I've made an experiment and removed while loop completely, so only these 2 lines left.

_WinAPI_ReadFile($hFile, DllStructGetPtr($tBuffer), 1, $nRead)
If $nRead > 0 Then $sRet &= DllStructGetData($tBuffer, 1)

Even without While command prompt waits for user input. So it seems that _WinAPI_ReadFile stops execution.

Link to comment
Share on other sites

Look, "con" file is a special file. Just take a look at . This code works. I was trying to modify it to set time limit for a user. It seems that I misunderstood how it works. I thought that while loop is running all the time capturing keystrokes. But using _WinAPI_ReadFile without while also waits for ENTER, so it seems I can't achieve what I want with this code.

So, I will rephrase my question: Is there a way in autoit to read user input from command prompt with some kind if time limit?

Edited by killgore
Link to comment
Share on other sites

  • 10 months later...
killgore

i also have same problem...

Func _ConsoleInput($sPrompt)
Local $Timer = TimerInit() ;Wait time
    If Not @Compiled Then Return SetError(1, 0, 0) ; Not compiled

    ConsoleWrite($sPrompt)

    Local $tBuffer = DllStructCreate("char"), $nRead, $sRet = ""
    Local $hFile = _WinAPI_CreateFile("CON", 2, 2)

    While 1
If TimerDiff($Timer)/1000 > 3 Then
Msgbox(0,TimerDiff($Timer)/1000,"")
_WinAPI_CloseHandle($hFile)
Return "y"
ExitLoop
EndIf
        _WinAPI_ReadFile($hFile, DllStructGetPtr($tBuffer), 1, $nRead)
        If DllStructGetData($tBuffer, 1) = @CR Then ExitLoop
        If $nRead > 0 Then $sRet &= DllStructGetData($tBuffer, 1)
Sleep(25)
    WEnd

    _WinAPI_CloseHandle($hFile)
    Return $sRet
EndFunc
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...