anyone write an ap that runs constantly in the background.

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
FireWorx
Posts: 362
Joined: Wed Sep 07, 2011 9:39 pm

anyone write an ap that runs constantly in the background.

Post by FireWorx » Sat Dec 24, 2011 2:21 am

Hi I was wondering if anyone had any experience relating to having a livecode ap that runs constantly in the background for instance checking to see if the contents of a folder has changesd and if so updating a database.

Would a acceptable plan be to on ap launch begin tracking the time and every 10 seconds check the folder contents of a folder and if the contents has changed then take action. Any potential problems with this?

Dave

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: anyone write an ap that runs constantly in the backgroun

Post by Mark » Sat Dec 24, 2011 5:05 am

Hi Dave,

Sure, you can do that. Use send in 10 secs to repeatedly check the value of the files function.

The only downside is that your hard disk will never ever spin down and will have a much shorter life span and if you'd drain the battery if using a laptop. A much better way would be to have the operating system send a message to your app when the folder changes. The Mac can do this with folder actions. I don't know how to do this on Windows and Linux. Maybe you could do it with AutoIt.

If you tell a little more about the purpose of your app, then I/we/someone might come up with a better alternative.

Kind regards,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10356
Joined: Wed May 06, 2009 2:28 pm

Re: anyone write an ap that runs constantly in the backgroun

Post by dunbarx » Sat Dec 24, 2011 5:29 am

What Mark said.

Make two stacks. In each make a button and a field. Put this into the button script of stack 1:

Code: Select all

on mouseup
   put 0 into fld 1
   doThis
end mouseup

on doThis
   if fld 1 > 20 then 
      put the pendingmessages into pend
      repeat for each line thisMessageID in pend
         cancel item 1 of thisMessageID
      end repeat
      exit to top
   end if
   add 1 to fld 1
   send "doThis" to me in 2 seconds
end doThis
In the button in stack 2:

Code: Select all

on mouseUp
  put random(99) into fld 1
end mouseUp
If you press the button in stack 1, you will start an incrementing value in the field. This will run while you navigate over to stack 2 (or anywhere else), where you can press the button all day to create random numbers in the stack 2 field. Go anywhere, the counter runs, and you can do your file thing instead of counting integers.

Eventually, the counter in stack 1 will stop. Read up on the pendingmessages to see how this works. Write back if you need to.

Craig Newman

FireWorx
Posts: 362
Joined: Wed Sep 07, 2011 9:39 pm

Re: anyone write an ap that runs constantly in the backgroun

Post by FireWorx » Sat Dec 24, 2011 8:29 am

Thanks for the info. A little more on the project.

The idea is to use live code to check every few seconds to see if a text file or text file(s) have been added to a collection folder on a windows PC. The text files will have a date time stamp for a file name. Runrev can put the name of the file into a container and check it against the last date timestamp that it processed and saved . If the folder contains a new text file then read 15 lines of data from the text file, launch an irev file on a web server to connect to the MySQL database and INSERT the 15 lines of data into the MySql database on the web server, then place the processed text file into a secondary historical collection folder. Leaving the first folder empty and waiting for another update.

Users in the field get a page letting them know there is new info that needs to be acted on. They click a button on their iPad and a Irev page is activated, processes a POST request to the web server db, and updates their screen with the data.

It's EMS emergency response data. The dispatch data will be printed via a text file to the folder as well as broadcast over the radio. Clicking the button on the iPad gives responders a hard copy of the dispatch info.

Dave

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: anyone write an ap that runs constantly in the backgroun

Post by Mark » Sat Dec 24, 2011 11:41 am

Hi Dave,

If there is a person who manually puts the text files into the folder, then I'd create a standalone and tell the user to drop the files on top of the standalone instead of the folder. The standalone grabs the file path, does what you want and eventually puts the file into the historical folder. This avoids the repeated checking of the folder. If the user does difficult about this, you could change the icon of the standalone into a folder icon ;-)

If the user doesn't put the files into the folder manually, then I'll need more details.

Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

FireWorx
Posts: 362
Joined: Wed Sep 07, 2011 9:39 pm

Re: anyone write an ap that runs constantly in the backgroun

Post by FireWorx » Sat Dec 24, 2011 4:57 pm

Thats a great idea that I will hold for the future! In this case however a text file is being sent across town to a printer. We are intercepting the text file and printing it to disk (electonicly placing it in the collection folder) and then passing the data along to the printer. We would like to get that hard copy out to the users in the field the users that are away from the printer.

The dispatchers that type in the data originaly dispatch units via CAD and send the data to our printer. They have other agencies they answer to and have little interest in altering there operation based on our needs. Hence the work around. Thanks for all your ideas. That gives me something to work with.

Dave

WaltBrown
Posts: 466
Joined: Mon May 11, 2009 9:12 pm

Re: anyone write an ap that runs constantly in the backgroun

Post by WaltBrown » Sat Dec 24, 2011 6:13 pm

Dave,
I've used a few scripts I've found online (including here) that I can, for example, use as macros within Outlook to extract certain emails to a file of key:value pairs placed in a designated inbox folder, then launch a standalone stack (sReader.exe in the example) that looks in the inbox for new files then parses any it finds, records their content, and fires off some other specific stack. Here's one I use, written in VBA:

Code: Select all

Sub TriggerReader(MyMail As MailItem)
    Dim strID As String
    Dim objMail As Outlook.MailItem
    Dim newFileName As String
    Dim objFSO
    Dim outFile
    Dim inboxPath As String
    Dim objShell
    
    On Error Resume Next
    
    inboxPath = "C:\AWork\InBox\"
    strID = MyMail.EntryID
    Set objMail = Application.Session.GetItemFromID(strID)
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    newFileName = inboxPath & "z" & objMail.EntryID & ".txt"
    Set outFile = objFSO.CreateTextFile(newFileName)
    outFile.writeLine "rSubj: " & objMail.Subject
    outFile.writeLine "rTo: " & objMail.To
    outFile.writeLine "rCC: " & objMail.CC
    outFile.writeLine "rName: " & objMail.SenderName
    outFile.writeLine "rAddr: " & objMail.SenderEmailAddress
    outFile.writeLine "rSent: " & objMail.SentOn
    outFile.writeLine "rRecip: " & objMail.Recipients -- NG, delete this line.
    outFile.writeLine "rCreation: " & objMail.CreationTime
    outFile.writeLine "rBody: " & objMail.Body
    outFile.writeLine "---------------------"
    outFile.writeLine "rHTML: " & objMail.HTMLBody
    outFile.writeLine "---------------------"
    outFile.Close
    
    Set objShell = CreateObject("WScript.shell")
    objShell.Run ("C:\AWork\sReader.exe")
    
    Set objMail = Nothing
    Set objFSO = Nothing
    Set outFile = Nothing
    Set objShell = Nothing
End Sub
I didn't create it, I extracted it from a number of examples I found on line, like MSDN.

I also recently read an article about using Windows Event Viewer to assign tasks to events that looks promising, but haven't tried it yet - while exploring, the number of available Windows events to drill into to figure which ones to use is daunting.
http://blogs.technet.com/b/wincat/arch ... vent.aspx
Walt Brown
Omnis traductor traditor

Post Reply