Sign in to follow this  
Followers 0
ADY2K14

Unexpected behaviour with loop

2 posts in this topic

Hi all,

My script does not work as intended and i cant figure out why. Its supposed to work like this: 

1) - Draw box on screen with message

2) - search a set directory for a txt file continuously

3) - As soon as file is found open and copy first line of txt. Hide box

4) - Display txt from file in message box.

5) - small delay

6) - delete file

7) - goback to 2 and repeat until user closes form

The delete is not working correctly and it seems the second time around the loop it just sits there even though there is a new file in the directory.

Any help appreciated

Thanks

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <GUIConstantsEx.au3>


Dim $match, $openfile, $SerialNo, $closeFile;declaration

while 1 ;- START MAIN LOOP

DrawBox()
GetSerial()
GUISetState(@SW_HIDE)
MsgBox($MB_SYSTEMMODAL, "", $SerialNo)
sleep(5000)
FileDelete("C:\test\" & "SN.txt")
WEnd ;- end of main loop
Exit

;***************************************************************************USER FUNCTIONS START HERE***************************************************************************************************************

Func GetSerial()
   local $filetofind = ("C:\test\" & "SN.txt")
   sleep(1000)
While 1

    $match = FileFindFirstFile($filetofind);check if file exists
   if $match = 1 Then
    $openfile = FileOpen($filetofind, 0) ; opening in read mode
    $SerialNo = FileReadLine($filetofind, 1) ; read 1st line
    $CloseFile = FileClose($filetofind)
    ExitLoop

 EndIf

 Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
            Exit
   EndSwitch

sleep (100) ;save CPU time
WEnd
EndFunc


Func DrawBox()
   ;Read serial
$Form1 = GUICreate("MyApp", 330, 100)
GUISetFont(20, 400) ; Set font size and colour
GUICtrlCreateLabel("Waiting For Serial Number", 10, 25)
WinMove($Form1, "",800, 10);Move window to top right corner
GUISetState(@SW_SHOW)
EndFunc

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

I didn't test this but see if this works:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <GUIConstantsEx.au3>

Global Const $Form1 = GUICreate("MyApp", 330, 100)

GUISetFont(20, 400)

GUICtrlCreateLabel("Waiting For Serial Number", 10, 25)

WinMove($Form1, "", 800, 10) ; Move window to top right corner

Global Const $filetofind = "C:\test\SN.txt"

Global $SerialNo

While 1
  GUISetState(@SW_SHOW)

  Sleep(1000)

  GetSerial()

  GUISetState(@SW_HIDE)

  MsgBox($MB_SYSTEMMODAL, "", $SerialNo)

  sleep(5000)

  FileDelete($filetofind)
WEnd

Func GetSerial()
  While 1
    If FileFindFirstFile($filetofind) <> -1 Then
      $SerialNo = FileReadLine($filetofind, 1)
      ExitLoop
    EndIf

    Switch GUIGetMsg()
      Case $GUI_EVENT_CLOSE
        Exit
    EndSwitch
  WEnd
EndFunc
Edited by jaberwacky

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