Jump to content

Help with loop


Recommended Posts

Hi guys,

I'm really new to this and i'm a little lost here, so any help will be appreciated.

I have made a simple form that reads from a textfile into labels.

now i need it to update from the textfile once every 3 minute and never stop doing so.

Here is my code so far.

 

#include <File.au3>
#include <ColorConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIConstants.au3>
#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>


Dim $sFilePath = "F:\input.txt" ; File to open and read from
$Formname=("InfoBox:")  ; Main Window Title

   ;
   Local $hFileOpen = FileOpen($sFilePath, $FO_READ)
    If $hFileOpen = -1 Then
        MsgBox($MB_SYSTEMMODAL, "", "An error occurred when reading the file.")
        Return False
    EndIf

   ; Read the lines of the file and saving them to Dims.
    Dim $sFileRead1 = FileReadLine($hFileOpen, 1) ;Line 1 = Date + time
    Dim $sFileRead2 = FileReadLine($hFileOpen, 2) ;Line 2 = Color from file
    Dim $sFileRead3 = FileReadLine($hFileOpen, 3) ;Line 3 = Actual Text 1 line
    Dim $sFileRead4 = FileReadLine($hFileOpen, 4) ;Line 4 = Actual Text 2 line


   FileClose($hFileOpen)


Output()

Func Output()
    ; Create a GUI with various controls.
    Local $hGUI = GUICreate($Formname, 1680, 360,1, 640,$WS_EX_DLGMODALFRAME) ;( "title" [, width [, height [, left = -1 [, top = -1 [, style = -1 [, exStyle = -1 [, parent = 0]]]]]]] )
      GUISetBkColor(0xFFFFFF)

 ; Create a label control.
    Local $datetime1 = GUICtrlCreateLabel( $sFileRead1 , 1, 5, 380, 40, BitOR($SS_Left,$SS_CENTERIMAGE)) ;( "text", left, top [, width [, height [, style = -1 [, exStyle = -1]]]] )
    GUICtrlSetFont($datetime1, 30, 0, 0, "")
    ; Set the color of the label control.
    GUICtrlSetColor($datetime1, $color_blue)

   ; Create a label control.
    Local $Label1 = GUICtrlCreateLabel( $sFileread3 , 1, 50, 1680, 120, BitOR($SS_Center,$SS_CENTERIMAGE)) ;( "text", left, top [, width [, height [, style = -1 [, exStyle = -1]]]] )
    GUICtrlSetFont($Label1, 70, 0, 0, "")
    ; Set the color of the label control.
    GUICtrlSetColor($Label1, $sFileread2)

    ; Create a label control.
    Local $Label2 = GUICtrlCreateLabel( $sFileRead4 , 1, 180, 1680, 120, BitOR($SS_Center,$SS_CENTERIMAGE)) ;( "text", left, top [, width [, height [, style = -1 [, exStyle = -1]]]] )
    GUICtrlSetFont($Label2, 70, 0, 0, "")
    ; Set the color of the label control.
    GUICtrlSetColor($Label2, $sFileread2)






    ; Display the GUI.
    GUISetState(@SW_SHOW, $hGUI)

    ; Loop until the user exits.
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE, $idClose
                ExitLoop

        EndSwitch
    WEnd

    ; Delete the previous GUI and all controls.
    GUIDelete($hGUI)
EndFunc   ;==>Output

 

Link to comment
Share on other sites

Welcome, I'd suggest not using Dim and switch to Local and Global. As for the every 3 minute problem, use a timer

Global $iTimer = TimerInit()

and check inside your while loop once it hits the 3 minute mark

While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE, $idClose
                ExitLoop
            Case Else
                ; 3 * 60000 is 3 minutes in milisecond format
                If (TimerDiff($iTimer) >= 3 * 60000) Then
                    ; Read all the information from the file again
                    GUICtrlSetData($datetime1, $sFileRead1)
                    GUICtrlSetColor($Label1, $sFileread2)
                    GUICtrlSetData($Label1, $sFileRead3)
                    GUICtrlSetData($datetime2, $sFileRead4)
                    
                    ; Reset the timer
                    $iTimer = TimerInit()
                EndIf
        EndSwitch
    WEnd

 

Link to comment
Share on other sites

InunoTaishou's way there is no need to recreate the GUI.

Or your way:

#include <ColorConstants.au3>
#include <GUIConstants.au3>
#include <FileConstants.au3>

Opt("TrayAutoPause", 0)

Global $sFilePath = "F:\input.txt" ; File to open and read from
Global $Formname = "InfoBox:" ; Main Window Title
Global $hGUI, $hFileOpen, $sFileRead1, $sFileRead2, $sFileRead3, $sFileRead4

While 1
    Output()
    Sleep(1000 * 60 * 3) ; 3 min
WEnd

Func Output()
    ; Delete the previous GUI and all controls.
    If IsHWnd($hGUI) Then GUIDelete($hGUI)
    $hFileOpen = FileOpen($sFilePath, $FO_READ)
    If $hFileOpen = -1 Then Return SetError(1, MsgBox(48, "", "An error occurred when reading the file.", 3), 0)
    ; Read the lines of the file and saving them to Globals.
    $sFileRead1 = FileReadLine($hFileOpen, 1) ;Line 1 = Date + time
    $sFileRead2 = FileReadLine($hFileOpen, 2) ;Line 2 = Color from file
    $sFileRead3 = FileReadLine($hFileOpen, 3) ;Line 3 = Actual Text 1 line
    $sFileRead4 = FileReadLine($hFileOpen, 4) ;Line 4 = Actual Text 2 line
    FileClose($hFileOpen)
    ; Create a GUI with various controls.
    $hGUI = GUICreate($Formname, @DesktopWidth, 360, 1, 640, $WS_EX_DLGMODALFRAME) ;( "title" [, width [, height [, left = -1 [, top = -1 [, style = -1 [, exStyle = -1 [, parent = 0]]]]]]] )
    GUISetBkColor(0xFFFFFF)

    ; Create a label control.
    Local $datetime1 = GUICtrlCreateLabel($sFileRead1, 1, 5, 380, 40, BitOR($SS_Left, $SS_CENTERIMAGE)) ;( "text", left, top [, width [, height [, style = -1 [, exStyle = -1]]]] )
    GUICtrlSetFont($datetime1, 30, 0, 0, "")
    ; Set the color of the label control.
    GUICtrlSetColor($datetime1, $color_blue)

    ; Create a label control.
    Local $Label1 = GUICtrlCreateLabel($sFileRead3, 1, 50, @DesktopWidth, 120, BitOR($SS_Center, $SS_CENTERIMAGE)) ;( "text", left, top [, width [, height [, style = -1 [, exStyle = -1]]]] )
    GUICtrlSetFont($Label1, 70, 0, 0, "")
    ; Set the color of the label control.
    GUICtrlSetColor($Label1, $sFileRead2)

    ; Create a label control.
    Local $Label2 = GUICtrlCreateLabel($sFileRead4, 1, 180, @DesktopWidth, 120, BitOR($SS_Center, $SS_CENTERIMAGE)) ;( "text", left, top [, width [, height [, style = -1 [, exStyle = -1]]]] )
    GUICtrlSetFont($Label2, 70, 0, 0, "")
    ; Set the color of the label control.
    GUICtrlSetColor($Label2, $sFileRead2)

    ; Display the GUI.
    GUISetState(@SW_SHOW, $hGUI)
    Return 1
EndFunc   ;==>Output

 

Regards,
 

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...