Jump to content

Using two continuous loop in a script


Recommended Posts

Am writing a program that its meant for a pet clinic management system its an assignment, once the detailed are entered its saves to a log file and then it sends the log to the email and the program need to run continuously, and the logs need to be sent continuously

Link to comment
Share on other sites

Okay, forgive me for being suspicious. I haven't used _InetSmtpMail, so I'm not entirely sure what the best solution to this would be. It seems there are three processes all trying to access the file at the same time. A simple solution would be to create a copy of the file and rename it. You could also include a timestamp in the name. Send the renamed copy. That way you don't need to worry about other processes accessing the main log file.

Check out FileCopy in the help file.

Edited by czardas
Link to comment
Share on other sites

Create a file called Current.log and a folder called Updates in the same directory as the script. Then edit the Currrent log file with notepad. When you run the code below a copy is created even though the file is being used by, and edited in, notepad (don't forget to save). I tested it and it works.

FileCopy ( "Current.log", "Updates/updated.log", 1)

oops forgot to include code. ;)

I'm still not entirely sure if this will work as it is. I need to think a little while.

Okay the following method should also work with your email script. The new file created is the one you need to send.

$filePath = "Updates/" & @YEAR & @MON & @MDAY & @HOUR & @MIN & @SEC & ".log"

FileCopy ( "Current.log", $filePath)

One more change added so you can create a new file every second if you wish (you only need an update every half hour). Please realise that you will need to clear out the folder with all these updated log files at some point. That's also something to think about. I think this method is acceptable because you don't want to have to exit the main program used by the clinic in order to send an email.

Edited by czardas
Link to comment
Share on other sites

$iMinutes = 1
$hTimer = TimerInit()
While 1
If TimerDiff($hTimer) > ($iMinutes * 60000) Then _INetSmtpMailCom($SmtpServer, $FromName, $FromAddress, $ToAddress, $Subject, $Body, $AttachFiles, $CcAddress, $BccAddress, $Importance, $Username, $Password, $IPPort, $ssl)
Wend

Link to comment
Share on other sites

It took me a while to find the for the code you are using. Please post links to any code you find in the Example Forum if you are using it.

Combining water's code with my own:

AdlibRegister("SendAttachment", 30 * 60 * 1000) ; Call this function every half hour.

While 1
    Sleep(1000) ; reduces cpu usage.
WEnd

Func SendAttachment()
    ; You need to create the file inside this function - which is called every half hour.
    ; Also all the other required variables need to be included somehow.
    $AttachFiles = "Updates/" & @YEAR & @MON & @MDAY & @HOUR & @MIN & @SEC & ".log"
    FileCopy ( "Current.log", $AttachFiles)
    _INetSmtpMailCom($SmtpServer, $FromName, $FromAddress, $ToAddress, $Subject, $Body, $AttachFiles, $CcAddress, $BccAddress, $Importance, $Username, $Password, $IPPort, $ssl)
EndFunc   ;==>Timer

You can declare the rest of the variables such as $FromName, $FromAddress etc inside the function SendAttachment() or you can declare them globally, which you probably are doing already anyway.. I also modified water's code so that the function kicks in every half an hour. You could integrate the rest of your code using this as a template.

Your (alternative) method is also acceptable and perhaps a little easier to implement, but you need to reset the timer every half an hour.

$hTimer = TimerInit()
While 1
    If TimerDiff($hTimer) > (30 * 60000) Then
        $AttachFiles = "Updates/" & @YEAR & @MON & @MDAY & @HOUR & @MIN & @SEC & ".log"
        FileCopy ( "Current.log", $AttachFiles)
        _INetSmtpMailCom($SmtpServer, $FromName, $FromAddress, $ToAddress, $Subject, $Body, $AttachFiles, $CcAddress, $BccAddress, $Importance, $Username, $Password, $IPPort, $ssl)
        $hTimer = TimerInit() ; reset the timer every half an hour.
    EndIf
Wend
Edited by czardas
Link to comment
Share on other sites

Sorry I edited my previous post quite a bit.

FileCopy ( "Current.log", $AttachFiles)

If i put this in the

while 1

as it the log is written there it automatically saves it to the $AttachFiles

Actually no. If you look at my second example above, the declaration takes place before the file has been created. The next line of code (which you posted) creates the file in the directory. In this case I called the directory Updates. Then the next line looks for the file in that directory and sends it by email. (in theory - since I haven't tried it) Edited by czardas
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...