Jump to content

Loop trouble


Recommended Posts

I have trouble getting my script right, the problem is that when $log is set to 1 the script writes to the logfile and does the shutdown (works OK)

but it will write to the logfile several times until the system has gone down.

I would need a way to make sure the logfile is written to only once before shutdown but I can't figure out how to do it.

Any help is appreciated, thanks

CODE
While 1

Sleep(1000)

$n = _GetLastInput()

If $n > $time Then

If $log=1 Then

;Write to logfile

$file = FileOpen(@ProgramFilesDir & "\Inact\inactlog.txt", 9)

FileWrite($file, @YEAR)

FileWrite($file, "-")

FileWrite($file, @MON)

FileWrite($file, "-")

FileWrite($file, @MDAY)

FileWrite($file, " ")

FileWrite($file, @HOUR)

FileWrite($file, ":")

FileWrite($file, @MIN)

FileWrite($file, "")

FileWrite($file, " action=")

FileWrite($file, $action)

FileWrite($file, @CRLF)

FileClose($file)

EndIf

Shutdown(5)

Else

EndIf

WEnd

Link to comment
Share on other sites

I have trouble getting my script right, the problem is that when $log is set to 1 the script writes to the logfile and does the shutdown (works OK)

but it will write to the logfile several times until the system has gone down.

I would need a way to make sure the logfile is written to only once before shutdown but I can't figure out how to do it.

Any help is appreciated, thanks

There is nothing in the Else portion of the IF statement, so I removed it, and _FileWriteLog() does everything for you, including the time stamp, so I substituted that in.

There should be no way for the shutdown not to happen after writing to the log unless you don't have permission in Windows to shut the machine down, so I put in an error check on the Shutdown:

#include <file.au3>

While 1
    Sleep(1000)
    $n = _GetLastInput ()
    If $n > $time Then
        If $log = 1 Then
            ;Write to logfile
            _FileWriteLog(@ProgramFilesDir & "\Inact\inactlog.txt", "Action=" & $action)
        EndIf
        
        If Not Shutdown(5) Then ; 5 = force shutdown
            MsgBox(16, "Error", "Failed to shutdown computer on command!")
        EndIf   
        Exit
    EndIf
WEnd

:)

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Thanx for your suggestions !

I modified the code to use _FileWriteLog and removed the Else statemant but the result is the same, the logfile is written 28 times until the actual shutdown action is completed.

I run my compiled script as a service and the shutdown action can be either logoff or power off and because it runs as a service I think there will be

problems if I simply put an Exit command after the Shutdown command.

Could it be that I need to exit the While loop when $n > $time is true and then write the logfile an perform the shutdown action ?

If thats the way to do it how would I do that and how do I then get back to the loop again after writing log file and doing a logoff ?

Link to comment
Share on other sites

Thanx for your suggestions !

I modified the code to use _FileWriteLog and removed the Else statemant but the result is the same, the logfile is written 28 times until the actual shutdown action is completed.

I run my compiled script as a service and the shutdown action can be either logoff or power off and because it runs as a service I think there will be

problems if I simply put an Exit command after the Shutdown command.

Could it be that I need to exit the While loop when $n > $time is true and then write the logfile an perform the shutdown action ?

If thats the way to do it how would I do that and how do I then get back to the loop again after writing log file and doing a logoff ?

Shutdown does not exit the script, so the loop keeps running until the process is closed (which may take a while). Did you notice the 'Exit' I put right after the Shutdown, and the MsgBox to indicate if you don't have permission to do that?

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
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...