JQadrad Posted August 13, 2010 Share Posted August 13, 2010 Hi, My second question on this forum while working on my project. In my script I check for files and accordingly to the file it finds, it displays the file in a label. E.g. left.txt is found and label is changed to left.txt. Then it finds the file right.txt (left.txt is deleted) and displays right.txt in the label. But while it is in this loop searching for files, it keeps changing between left.txt and right.txt, but when I press stop, it always displays the last file found. When I continue the loop again without exiting the program it keeps changing between those 2 files. When I have three files, it keeps changing between those three. Why does this happen? Should I clear a buffer or..? I hope my question is clear, Regards, JJ Link to comment Share on other sites More sharing options...
AdmiralAlkex Posted August 13, 2010 Share Posted August 13, 2010 Clear as mud perhaps. When asking for help you should post a reproducer. Short and runnable/working. Not only does that make it possible for us to know WTF you're talking about, you will often find the problem/answer the question yourself when writing said reproducer. .Some of my scripts: ShiftER, Codec-Control, Resolution switcher for HTC ShiftSome of my UDFs: SDL UDF, SetDefaultDllDirectories, Converting GDI+ Bitmap/Image to SDL Surface Link to comment Share on other sites More sharing options...
JQadrad Posted August 13, 2010 Author Share Posted August 13, 2010 Clear as mud perhaps. When asking for help you should post a reproducer. Short and runnable/working. Not only does that make it possible for us to know WTF you're talking about, you will often find the problem/answer the question yourself when writing said reproducer. Okay, let me try again. This is the code: expandcollapse popup#include <GUIConstantsEx.au3> Opt('MustDeclareVars', 1) Opt("GUIOnEventMode", 1) Global $checkfile1 ;~ Overige items Global $startID, $stopID, $ExitID, $form Global $action Global $label_player1 $action = FALSE _Main() Func _Main() local $version $version = "Alpha 0.2"; $form = GUICreate("BEP interface - 2010 - " & $version, 460, 300) $checkfile1 = FALSE ;~ Start knop $startID = GUICtrlCreateButton("Start", 30, 120, 50, 20) GUICtrlSetOnEvent($startID, "onstart") ;~ Stop knop $stopID = GUICtrlCreateButton("Stop", 90, 120, 50, 20) GUICtrlSetOnEvent($stopID, "onstop") GUISetOnEvent($GUI_EVENT_CLOSE, "OnExit") GUISetState() While(1) If($action = TRUE) Then CheckFile1() EndIf WEnd EndFunc Func onstart() $action = TRUE; EndFunc Func onstop() $action = FALSE; EndFunc Func CheckFile1() Local $player1files_array[5] Local $actie_player1 $player1files_array[1] = "left1.txt" $player1files_array[2] = "right1.txt" $player1files_array[3] = "up1.txt" $player1files_array[4] = "down1.txt" For $files_1 in $player1files_array If FileExists($files_1) Then Switch($files_1) Case "left1.txt" $actie_player1 = "Left" Case "right1.txt" $actie_player1 = "Right" Case "up1.txt" $actie_player1 = "Up" Case "down1.txt" $actie_player1 = "Down" EndSwitch FileDelete($files_1) Else EndIf Next $label_player1 = GUICtrlCreateLabel($actie_player1, 10, 220, 50, 15) EndFunc Func OnExit() Exit EndFunc What is does.. While $action is TRUE, it scans the folder where the script is located for 4 files, left1.txt, right1.txt, up1.txt and down1.txt. If left1.txt is found, it should set $label_player1 to Left. When right1.txt is, it should display Right. When the file is found, it is deleted. Another (external) application creates the 4 files, so they are recreated in random order. When disabling the file deletion, so left1.txt keeps existing, the label stays on Left. When I rename left1.txt to right1.txt, while the program is running, the label switches between Left and Right. When you press stop, it displays Right. When you continue again, it will switch between Left and Right again. My question is why this is happening. I hope this is more clear then my previous attempt. Excuse me if my code is messy =) Link to comment Share on other sites More sharing options...
smashly Posted August 14, 2010 Share Posted August 14, 2010 (edited) Not sure why it's happening, but you could just make your Switch/EndSwitch redundant. Also your Creating a label over and over which while eventually cause the script to crash. Another thing would be to add a small Sleep(10) in your While/WEnd loop to stop the cpu hogging of your script. Maybe you can try something like this..expandcollapse popup#include <GUIConstantsEx.au3> #include <WindowsConstants.au3> Opt('MustDeclareVars', 1) Opt("GUIOnEventMode", 1) Global $checkfile1 ;~ Overige items Global $startID, $stopID, $ExitID, $form Global $action Global $label_player1 Global $aPlayer1[5][2] = [[4],["left1.txt", "Left"], ["right1.txt", "Right"],["up1.txt", "Up"],["down1.txt", "Down"]] $action = FALSE _Main() Func _Main() local $version $version = "Alpha 0.2"; $form = GUICreate("BEP interface - 2010 - " & $version, 460, 300, -1, -1, -1, $WS_EX_TOPMOST) $label_player1 = GUICtrlCreateLabel("", 10, 220, 50, 15) $checkfile1 = FALSE ;~ Start knop $startID = GUICtrlCreateButton("Start", 30, 120, 50, 20) GUICtrlSetOnEvent($startID, "onstart") ;~ Stop knop $stopID = GUICtrlCreateButton("Stop", 90, 120, 50, 20) GUICtrlSetOnEvent($stopID, "onstop") GUISetOnEvent($GUI_EVENT_CLOSE, "OnExit") GUISetState() While(1) Sleep(10) If($action = TRUE) Then CheckFile1() WEnd EndFunc Func onstart() $action = TRUE; EndFunc Func onstop() $action = FALSE; EndFunc Func CheckFile1() For $i = 1 To $aPlayer1[0][0] If FileExists($aPlayer1[$i][0]) Then If GUICtrlRead($label_player1) <> $aPlayer1[$i][1] Then GUICtrlSetData($label_player1, $aPlayer1[$i][1]) ;Enable the line below for file delete. Just testing renaming the file in windows. ;~ FileDelete(@ScriptDir & "\" & $aPlayer1[$i][0]) ExitLoop EndIf Next EndFunc Func OnExit() Exit EndFuncDisabling the FileDelete() and renaming the file in windows does what it should for me with the script started or stopped Cheers Edited August 14, 2010 by smashly Link to comment Share on other sites More sharing options...
JQadrad Posted August 14, 2010 Author Share Posted August 14, 2010 That works nice. Thanks for the reply. I can proceed now. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now