PDA

View Full Version : Communicating between 2 Thinbasic scripts



martin
30-07-2009, 11:34
Hi Guys,

My script does downloads a bunch of urls and analyzes the HTML code of each downloaded page. First, I did this with INET_URLGETSTRING. But this made the UI very slow (the application was reacting very slow if I resized the window or when I clicked a button). Then Eros created a better way to read internet pages with new commands INET_Internet_OpenUrl and INET_Http_QueryInfo. This works great but NOT if you open a lot of urls after each other. I guess INET_Internet_OpenUrl does not has any DOEVENTS inside. So the UI was still extremely slow when I was running my script.

To solve this I created another TB script that runs in the background (script runs with a SHELL_EXECUTE command in the main script). With a DO - LOOP contruction in both scripts I communicate between both scripts by setting and reading the clipboard. So in my main script I sent a "download command" by setting the clipboard to "MMpLAYERurl - www.thisurl.com/index.html" and in the background script I send a command (and html output) back if the download has been completed. The main script waits for this command in a DO/LOOP.


CODE MAIN SCRIPT:

function download(sUrl as string) as string
local t as string

clipboard_settext "MMpLAYERurl - " & sURL

do
if left$(ClipBoard_GetText,14)="MMpLAYERout - " then
t=mid$(ClipBoard_GetText,15)
exit do
end if

doevents
loop

clipboard_settext " "
function=t
end function


CODE BACKGROUND SCRIPT:

uses "inet"

function tbmain()
local t as string

local hMutex As DWORD = APP_MutexCreate("BackgroundApp" & app_scriptname)
If hMutex = 0 Then 'an instance of background.tbasic is already running
APP_MutexClose(hMutex)
exit function
end if


do
if left$(ClipBoard_GetText,14)="MMpLAYERurl - " then
t=inet_urlgetstring(mid$(ClipBoard_GetText,15))
t=acode$(UTF8ToWideChar$(t))
t="MMpLAYERout - " & t
end if

ClipBoard_setText t

if clipboard_gettext = "MMpLAYERend - " then exit do

doevents
loop

ClipBoard_setText " "
APP_MutexClose(hMutex)
end function


This works nice, the UI is ultra fast now. But sometimes there seems to go something wrong with this way of communicating: the clipboard is sometimes empty (I don't know why) and then the script goes into a endless loop... :violent:

So nice try, but there must be a better way to communicate between two scripts. Can I send/recieve variables (instead of clipboard) and should I use DECLARE command then, and how?

Thanks,

Martin

Michael Clease
30-07-2009, 14:53
Why not use the tcpudp module to get them to chat and you can send it commands to download for you echo back when its loaded.

martin
30-07-2009, 15:03
Why not use the tcpudp module to get them to chat and you can send it commands to download for you echo back when its loaded.


Hi Michael, sounds as a good idea to me! But wouldn't that be the same as writing/reading a variable to/from a file? I will give it a try...

Petr Schreiber
30-07-2009, 17:39
Hi Martin,

I think the TCPUDP communication is the best way.

There are some ... alternatives, you can see my interprocess communication demo here (http://community.thinbasic.com/index.php?topic=1671.msg12108#msg12108), but the way Michael proposes seems the most flexible to me.


Petr