Jump to content
Sign in to follow this  
MariusN

Freezing problem with code

Recommended Posts

MariusN

Does anyone here have an idea where the prob lies? This program will work for about 30 min or so, and then just totally "freeze". Now i dunno if it has something to do with the netstat command. I have changed the functions, moved some commands to start differently etc. There might be more declares than variables,but thats just how i tried to get it to stop freezing...will attend to the tweaking of the code later. Here is the complete code (so far)

#RequireAdmin
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=..\..\Icons\Alpha Dista Icon 15.ico
#AutoIt3Wrapper_Outfile=bandmon.exe
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <GUIConstantsEx.au3>
#include <File.au3>
#include <Constants.au3>
#include <Timers.au3>
Opt('MustDeclareVars', 1)
Opt("TrayMenuMode", 3)


Local $i_edit3, $i_edit4, $grandtotal2, $h_edit3, $button1, $fileLog, $fileLog2, $open11, $open12, $read3, $readtest
Local $var1, $var2, $var3, $var4, $rnd1, $rnd2, $rnd3, $rnd4, $read11, $test, $i_edit, $i_edit2, $h_edit, $h_edit2, $MSG
Local $i_timer, $i_pid, $s_stdout, $receive, $send, $open1, $read8, $open4, $write2, $open5, $read9, $open2, $read2, $open3
Local $zero, $open6, $open7, $read10, $write1, $read1, $total1, $total2, $total3, $check, $read4, $read3, $i_pid2
Local $s_stdout2, $open8, $read5, $view, $exititem, $list, $i, $font, $MSG2,$timer2

$rnd3 = $rnd1 + $rnd2
$view = TrayCreateItem("View Live Feed")
$exititem = TrayCreateItem("Exit")

$fileLog = (@ScriptDir & "\nrd3.txt")
$fileLog2 = (@ScriptDir & "\total.log")
$list = ProcessList("BandMon.exe")
If $list[$i][0] > 1 Then
    MsgBox(0, "Ooops...", "Program already Active!", 2)
    Exit
EndIf

Local $s_ip = @IPAddress1

$font = "Times New Roman"


If FileExists(@ScriptDir & "\nrd3.txt") Then
Else
    _FileCreate(@ScriptDir & "\nrd3.txt")
EndIf

If FileExists(@ScriptDir & "\total.log") Then
Else
    _FileCreate(@ScriptDir & "\total.log")
EndIf
_FileWriteToLine($fileLog, 1, "", 1)

$timer2 = _Timer_Init()

While 1
    If _Timer_Diff($timer2) / 1000 >= 2 Then

        ;$i_pid2 = Run(@ComSpec & " /c netstat -e", @SystemDir, @SW_HIDE, 0x8)
        $i_pid2 = Run("netstat -e", @SystemDir, @SW_HIDE, 0x8)
        While 1
            $s_stdout2 &= StdoutRead($i_pid2)
            If @error Then
                ExitLoop
            EndIf
        WEnd

        If $s_stdout2 Then
            $receive = StringMid($s_stdout2, 100, 17)
            $send = StringMid($s_stdout2, 115, 17)
            $var1 = $receive / 1048576
            $rnd1 = Round($var1, 2)
            $var2 = $send / 1048576
            $rnd2 = Round($var2, 2)
            $rnd3 = $rnd1 + $rnd2

            ;==============================================================================================

            If $read1 = $rnd3 Then
            Else

                $open1 = FileOpen($fileLog, 0)
                $read1 = FileReadLine($open1, 1) ; Total1
                FileClose($open1)

                $open2 = FileOpen($fileLog, 2)
                $write1 = FileWriteLine($open2, $rnd3) ;Total2
                FileClose($open2)
                $total1 = $rnd3

                $open3 = FileOpen($fileLog2, 0)
                $read2 = FileReadLine($open3, 1)
                FileClose($open3)

                $open4 = FileOpen($fileLog2, 2)
                FileWriteLine($open4, $read2 + $rnd3 - $read1)
                FileClose($open4)

                $open6 = FileOpen($fileLog2, 0)
                $read4 = FileReadLine($open6, 1)
                FileClose($open6)

            EndIf
            $open8 = FileOpen($fileLog2, 0)
            $read5 = FileReadLine($open8, 1)
            FileClose($open8)
            TraySetToolTip("Total MBytes to date " & $read5 & " MBytes")
            $s_stdout2 = ""
            ;============================================================================================
        EndIf

        $timer2 = _Timer_Init()
    EndIf
    _tray()
WEnd


Func _Start()
    GUICreate("BandMon v1.00 on PC -= IP: " & $s_ip & " =-", 450, 450, -1, 20)
    GUICtrlCreateLabel("Receive: (Mb)", 220, 30, 120, 20)
    GUICtrlCreateLabel("Send: (Mb)", 330, 30, 120, 20)
    GUICtrlCreateLabel("Total: (Mb)", 220, 80, 120, 20)
    GUICtrlCreateLabel("Curently Send/Receive Bytes...", 10, 50, 220, 20)
    GUICtrlSetFont(-1, 11, 600, 0, $font)
    GUICtrlSetColor(-1, 0x3333FF)
    GUICtrlCreateLabel("Total Bytes since PC Startup...", 10, 100, 220, 20)
    GUICtrlSetFont(-1, 11, 600, 0, $font)
    GUICtrlSetColor(-1, 0x3333FF)
    GUICtrlCreateLabel("Total Bytes This Month...", 10, 150, 220, 20)
    GUICtrlSetFont(-1, 11, 600, 0, $font)
    GUICtrlSetColor(-1, 0x3333FF)
    $button1 = GUICtrlCreateButton("Reset ", 10, 250, 60, 20)

    $i_edit = GUICtrlCreateLabel("", 220, 50, 200, 20, $SS_SUNKEN)
    $i_edit2 = GUICtrlCreateLabel("", 220, 100, 100, 20, $SS_SUNKEN)
    $i_edit3 = GUICtrlCreateLabel("", 220, 150, 100, 20, $SS_SUNKEN)

    GUISetState()
    $h_edit = GUICtrlGetHandle($i_edit)
    $h_edit2 = GUICtrlGetHandle($i_edit2)
    $h_edit3 = GUICtrlGetHandle($i_edit3)



    While 1
        $MSG = GUIGetMsg()
        Select
            Case $MSG = $GUI_EVENT_CLOSE
                ExitLoop
            Case $MSG = $button1

                $open11 = FileOpen($fileLog, 2)
                FileWriteLine($open11, "0")
                FileClose($open11)
                Sleep(20)
                $open12 = FileOpen($fileLog2, 2)
                FileWriteLine($open12, "0")
                FileClose($open12)
                Sleep(20)
        EndSelect
        If _Timer_Diff($timer2) / 1000 >= 2 Then
            ;$i_pid = Run(@ComSpec & " /c netstat -e", @SystemDir, @SW_HIDE, 0x8)
            $i_pid = Run("netstat -e", @SystemDir, @SW_HIDE, 0x8)
            While 1
                $s_stdout &= StdoutRead($i_pid)
                If @error Then
                    ExitLoop
                EndIf
            WEnd
            If $s_stdout Then
                $receive = StringMid($s_stdout, 100, 17)
                $send = StringMid($s_stdout, 115, 17)

                WinSetState($h_edit, "", @SW_LOCK)
                WinSetState($h_edit2, "", @SW_LOCK)
                WinSetState($h_edit3, "", @SW_LOCK)
                GUICtrlSetData($i_edit, "")
                GUICtrlSetData($i_edit2, "")
                GUICtrlSetData($i_edit3, "")
                $var1 = $receive / 1048576
                $rnd1 = Round($var1, 2)
                $var2 = $send / 1048576
                $rnd2 = Round($var2, 2)
                $rnd3 = $rnd1 + $rnd2

                ;==============================================================================================

                If $read1 = $rnd3 Then
                Else

                    $open1 = FileOpen($fileLog, 0)
                    $read1 = FileReadLine($open1, 1) ; Total1
                    FileClose($open1)

                    $open2 = FileOpen($fileLog, 2)
                    $write1 = FileWriteLine($open2, $rnd3) ;Total2
                    FileClose($open2)
                    $total1 = $rnd3

                    $open3 = FileOpen($fileLog2, 0)
                    $read2 = FileReadLine($open3, 1)
                    FileClose($open3)

                    $open4 = FileOpen($fileLog2, 2)
                    FileWriteLine($open4, $read2 + $rnd3 - $read1)
                    FileClose($open4)

                    $open6 = FileOpen($fileLog2, 0)
                    $read4 = FileReadLine($open6, 1)
                    FileClose($open6)

                EndIf
                $open5 = FileOpen($fileLog2, 0)
                $read3 = FileReadLine($open5, 1)
                FileClose($open5)
                ;===================================================================================================
                GUICtrlSetData($i_edit, "    " & $rnd1 & "                         " & $rnd2)
                GUICtrlSetData($i_edit2, "    " & $rnd3)
                GUICtrlSetData($i_edit3, "    " & $read3)
                WinSetState($h_edit, "", @SW_UNLOCK)
                WinSetState($h_edit2, "", @SW_UNLOCK)
                WinSetState($h_edit3, "", @SW_UNLOCK)

                $s_stdout = ""
            EndIf
            $timer2 = _Timer_Init()
        EndIf
    WEnd
    GUIDelete()
EndFunc   ;==>_Start



Func _tray()

    TraySetState()
    While 1
        If _Timer_Diff($timer2) / 1000 >= 2 Then
            ExitLoop
        EndIf
        $MSG2 = TrayGetMsg()
        Select
            Case $MSG2 = $view
                _Start()
            Case $MSG2 = $exititem
                Exit
        EndSelect
    WEnd
EndFunc   ;==>_tray
Edited by MariusN

Share this post


Link to post
Share on other sites
martin

I would advise against having a function _Tray being able to call _Start if the main gui already exists.

Instead of the

If Timerdiff

in your main while loop I would have

While processExists($_pid2)

processClose($i_pid2)

sleep(100)

wend

to ensure netstat is not running more than once. Maybe you could use TaskManager to see if there is more than one instance running when you have a problem.


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites
MariusN

thx for the advice Martin, but at a certain stage i was running ONLY the Tray with 1 function imbedded in it's loop as shown below

While 1
    If _Timer_Diff($timer2) / 1000 >= 2 Then

        ;$i_pid2 = Run(@ComSpec & " /c netstat -e", @SystemDir, @SW_HIDE, 0x8)
        $i_pid2 = Run("netstat -e", @SystemDir, @SW_HIDE, 0x8)
        While 1
            $s_stdout2 &= StdoutRead($i_pid2)
            If @error Then
                ExitLoop
            EndIf
        WEnd

        If $s_stdout2 Then
            $receive = StringMid($s_stdout2, 100, 17)
            $send = StringMid($s_stdout2, 115, 17)
            $var1 = $receive / 1048576
            $rnd1 = Round($var1, 2)
            $var2 = $send / 1048576
            $rnd2 = Round($var2, 2)
            $rnd3 = $rnd1 + $rnd2

            ;==============================================================================================

            If $read1 = $rnd3 Then
            Else

                $open1 = FileOpen($fileLog, 0)
                $read1 = FileReadLine($open1, 1) ; Total1
                FileClose($open1)

                $open2 = FileOpen($fileLog, 2)
                $write1 = FileWriteLine($open2, $rnd3) ;Total2
                FileClose($open2)
                $total1 = $rnd3

                $open3 = FileOpen($fileLog2, 0)
                $read2 = FileReadLine($open3, 1)
                FileClose($open3)

                $open4 = FileOpen($fileLog2, 2)
                FileWriteLine($open4, $read2 + $rnd3 - $read1)
                FileClose($open4)

                $open6 = FileOpen($fileLog2, 0)
                $read4 = FileReadLine($open6, 1)
                FileClose($open6)

            EndIf
            $open8 = FileOpen($fileLog2, 0)
            $read5 = FileReadLine($open8, 1)
            FileClose($open8)
            TraySetToolTip("Total MBytes to date " & $read5 & " MBytes")
            $s_stdout2 = ""
            ;============================================================================================
        EndIf

        $timer2 = _Timer_Init()
    EndIf
   
WEnd

...and i still had the problem (Without the GUI)

I got a "hunch" that it has something to do with the NETSTAT command, but i'll def try your idea as well :mellow:

Edited by MariusN

Share this post


Link to post
Share on other sites
MariusN

Got it working 100% now without a freeze. The problem was that the commands following netstat was too quick...After adding a sleep of 100 after the command, program is stable :mellow:

Edited by MariusN

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  

×