Sign in to follow this  
Followers 0
killgore

Problem with TimerDiff

9 posts in this topic

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?

Share this post


Link to post
Share on other sites



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

√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
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. :)

Share this post


Link to post
Share on other sites

Hi,

Are you using the $start variable elsewhere?

Br, FireFox.


 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Does the CON file exist?

Have you tried in administrator mode? (#RequireAdmin function)

Br, FireFox.


 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

That's maybe why I don't understand what the command prompt does here.


 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
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
Sign in to follow this  
Followers 0