Background Task

Blitz3D Forums/Blitz3D Beginners Area/Background Task

leeluna(Posted 2003) [#1]
Could blitz be used to write a program that sits in the background and only does anything when a certain type of file is created by another application? E.G.

Start the Blitz code
Blitz code checks directory for files ending in .CTA
Repeat
If files found
process them
Else
Sleep without using loads of memory
Endif
Forever

sort of thingy????

Luna


Perturbatio(Posted 2003) [#2]
you would need to use win API calls.
Take a look for FindFirstChangeNotification, WaitForMultipleObjects and FindNextChangeNotification.


leeluna(Posted 2003) [#3]
Perturbation,

Are they Blitz3d functions?

Luna


Perturbatio(Posted 2003) [#4]
no they are Windows API calls (requires winAPI userlibs)

I would steer clear of them if you know not what you are doing.


leeluna(Posted 2003) [#5]
Count me as steered clear.

Thanks.


Perturbatio(Posted 2003) [#6]
Don't get me wrong, you should try and learn API calls at some point, but if you've never done it before, doing it from blitz is not going to be the easiest thing.


soja(Posted 2003) [#7]
leeluna, yes, you could do that with minimal effort (and minimal CPU usage) in BlitzPlus. API calls are not necessary.


leeluna(Posted 2003) [#8]
Perturbatio,
I will still look into API, I found a few good posts on this site which will point me in the right direction but for my present project I will leave well enough alone.

Soja,

Dang and I went and bought B3D !!!

Luna


soja(Posted 2003) [#9]
BlitzPlus would be much more suited to that than Blitz3D, but never fear, Blitz3D is useful in its own right!


CyberPackRat(Posted 2003) [#10]
Don't poo-poo Blitz3D.. it rocks.


Perturbatio(Posted 2003) [#11]
I can think if one way of doing it in B3D without API calls, but it might not have a very low cpu usage.

Basically, write a function to scan the directory for the filetype, and call it once every few seconds.

The advantage of the API calls is that they will tell you when the directory has changed as opposed to you manually checking.


soja(Posted 2003) [#12]
Tremens - Er... the right tool for the right job, and all that... Blitz3D rocks for making games, but not really for transparent background tasks.

Pertubatio - For an API function to tell you something when an event occurs, you would need to register a callback function with it, right? Have you figured out a way to handle that in Blitz without writing your own DLL in another language? The way I figure it, you're going to have to keep polling either way, whether through Blitz or a WinAPI function. (And in BlitzPlus, with its event-based architecture, you could do that easily with low CPU and I/O usage.)


darklordz(Posted 2003) [#13]
*friendly reminder, blitz is a gameprogramming lang, suited best for game programming. To use blitz in the background would be foolish because the exe eats ur system resources....


soja(Posted 2003) [#14]
* A qualification: Darklordz is probably talking about BlitzBasic and Blitz3D, and he's right. However, using BlitzPlus for a background application falls completely within the bounds of what it was meant for, and will be very easy on your system resources.


leeluna(Posted 2003) [#15]
People,

After reading your responses I am very tempted to purchase B+, Will this conflict with my B3D installation or should everything be OK?.

I purchased B3D as soon as I heard about it on the PC and had dreams of controlling my PC with the ease of controlling my old amiga. Like everyone I would love to create my own game but I always seem to end up coding stuff for work which has nothing to do with shooting anything!!!
.
Thanks
Luna


eBusiness(Posted 2003) [#16]
No conflict at all, exept, if you open a .bb file you cannot make your coputer predict if it should use B+ or B3D.

For the original topic, it all depends, how large an area should it check and how often, if you put all your .CTA files in one directory and want it to check every 10 secs I don't think it will be a problem, seems like a b3D program take at least 6 MB of your ram.


Kanati(Posted 2003) [#17]
I have bb, b3d, and b+ all installed. All work fine with each other.


leeluna(Posted 2003) [#18]
eBusiness,

Thanks for the info, All the .CTA files will be in the same directory and I will be checking for them every 20 - 30 minutes and then starting another task depending on the .CTA file contents( ExecFile() ).

Thanks again
Luna


jfk EO-11110(Posted 2003) [#19]
It's ok to buy B+ since it offres a lot of useful things for apps not found in B3D, but I think what you need is dead simple and could be done in blitz easily.

It isn't that hard to get into simple API Calls using Blitz .decls.

You need the command ShowWindow that can be used to make the Blitz app invisible. Blitz will not use that big lot of memory, compared to a preloaded MSIE in memory or something, I wouldn't worry too much about it. Blitz is not using too much of CPU power if you use the "delay" command in a clever way.

All you have to do is:
Run the app in windowed mode
Using ShowWindow hwnd,0 to hide the task

delay 10000
check if it's time for a new .CTA Check (every 20 Minutes as you say)
if so, read the directory and search for the .CTA files
if you want to know if a file with the same name has been edited sine the last time then check the checksum


.decls: these are files you have to store inside the "userlibs" folder of your Blitz3D folder.

they contain definitions of Api (and other DLL) -calls you want to be accsessible in Blitz. They start with the definition of the DLL that contains the calls. A list of call-commands then follows.

the ShowWindow call needs to be defind this way:

Since the Windows System-API call "ShowWindow" is part of the "user32.dll" Dynamic Link Library ou have to use a file named "user32.decls" for this. The file looks like this:
.lib "user32.dll"
FindWindow%( class$,Text$ ):"FindWindowA"
ShowWindow(hwnd%,nCmdShow%)
GetActiveWindow%()
GetDC%(hWnd% )
ReleaseDC%(hWnd%,hDC%)
GetDesktopWindow%()
GetSystemMetrics%(nIndext%)
GetWindowTextLength%(hwnd):"GetWindowTextLengthA" 
GetWindowText%(hwnd%,buff*,anzahl%):"GetWindowTextA" 
SetWindowText(hwnd%,buffer$):"SetWindowTextA"


So here you can see the command ShowWindow along with some other useful Api-calls.

One important thing with Api calls is the "hwnd" which is the window-handle of the running app. If your app want to use Api-calls to midify itself then it needs to know its own Window-Handle. There are several ways to determine the hwnd, the esiest would be to call GetActiveWindow(), but it is not very secure since an other window could become "the active window" meanwhile. It's better to use FindWindow that is using the app class and the app title.

the class is diffrent with the several Blitz Products, however, here is the code to hide the window:

; Const class$="GX_WIN32_CLASS"         ; <- BlitzPlus 1.11
; Const class$="BLITZMAX_WINDOW_CLASS"  ; <- BlitzPlus 1.34
Const class$="Blitz Runtime Class"    ; <- Blitz3D

Global title$="My Blitz Window" ; starting title
AppTitle title$

hwnd=FindWindow(class$,title$)

WaitKey()
Delay 500
FlushKeys()
ShowWindow hwnd,0 ; hide it

; do anything in the background

Delay 5000
ShowWindow hwnd,1 ; show it
WaitKey()
End



I hope this helps.


soja(Posted 2003) [#20]
Note that jfk's example is for Blitz3D only. I think there are some issues regarding the difference between BlitzPlus and Blitz3D worth paying attention to: (1) Using the Delay command results in your application being unresponsive to Windows. This is unacceptable if you wish your application to be multitasked (and IMO windowed apps should always play nice in a multitasking environment). BlitzPlus would use the WaitEvent command in an event loop, which effectively puts the app to sleep, though it still listens for messages from Windows, so it will respond when you try to minimize/maximize/move/close/re-paint it. (2) You don't need to use the Windows API ShowWindow in BlitzPlus because there's already a command to do the same thing (e.g. HideGadget/ShowGadget). In fact, you also have the option of not even using a window at all.

Of course you can't get around those issues with Blitz3D -- BUT if you're just looking for simple functionality (and don't want to pay $60 yet), you can do it that way.

The program you want could probably be written well in around 50 lines of BlitzPlus code.


soja(Posted 2003) [#21]
leeluna, I wrote up this program really quick which does the basics of what you want in BlitzPlus. It uses a fraction of 1% of my CPU time.
Const WaitTime% = 10 ; 10 Seconds
Const Folder$ = "C:\"
CreateTimer(1)
Repeat
	Select WaitEvent()
		Case $4001 ; Timer tick (one second) has occurred
			ticks% = ticks + 1
			; if WaitTime has passed then check the folder for *.CTA files
			If ticks Mod WaitTime = 0 Then CheckFolder(Folder)
	End Select
Forever

Function CheckFolder(Folder$)
	; Find all CTA files in the folder
	dir = ReadDir(Folder)
	While MoreFiles(dir)
		; Found a file.  Is it CTA file?
		file$ = NextFile$(dir)
		If Upper$(Right$(file$, 4)) = ".CTA" Then CheckCTA(file)
	Wend
	CloseDir(dir)
End Function

Function CheckCTA(file$)
	; Check to see if we've seen this CTA file before
	For cta.CTAFiles = Each CTAFiles
		If cta\Name = file Then Return
	Next
	; It's a new one
	cta.CTAFiles = New CTAFiles
	cta\Name = file
	Notify "Found '"+file$+"'... perform ExecFile stuff here..."
End Function

Type CTAFiles
	Field Name$
End Type



leeluna(Posted 2003) [#22]
JFK, Soja.

Thanks to both of you for helping me out, I will be purchasing B+ to do this code as I intend to install it at my work place and dare not take risks with API - .decls. which I to be honest have no knowledge of, The last thing I need to do is end my career by melting down my work place servers.

Thanks loads for the help and info.

Luna


leeluna(Posted 2003) [#23]
OK I have taken the plunge and am sitting all excited waiting for my product to arrive!!!.

Hhhmm how long does this usually take?? tick tick tick...

Luna.