Background task with in application - is this possible?
Posted
#1
(In Topic #1282)
Enthusiast

Just wanted to run a quick question past you all
I have a app that create data files at the end of a sale but it take 2-4 seconds to make them all and this can slow down the checking out of customers
What I was wondering is can this function be moved to a background task?
Basically I am renaming .tmp files that are created during the transition to files that are used by data processor app.
For example
I have a file called journal.temp and this holds all the sale data once the sale is completed it would be renamed to 001000001.jor (or what ever the transaction number is)
I am open to ideas as how to speed this up as a few customers are saying this is the only bottle neck on the system.
If you need to see any given part of the code let me know
Thanks for any input you all may have
Andy
Posted
Guru

Posted
Enthusiast

BruceSteers said
Yea I've been reading up on that but there is no code Example as how to use it.
Posted
Guru

Did you read the help page? /comp/gb/task - Gambas Documentation
The instructions are all there or can you only copy code?
Make a class and make it Inherit Task.Class
Then you just instantiate the class and it starts the background process.
I think Cogier has an example on the Farm.
Or just make a shell script to rename the files and run that with Exec asynchronously (do not use Wait)
Posted
Regular

Posted
Guru

A simple Task example.
On load it just reads the HOME dir and gets filenames and icons in the background.
then the filename and icon picture are picked up by the Read event and added to a GridView.
I Save the picture to a temp file then load it with File.Load() then convert it to a Base64 String in the background task ,
then it's safe to Print as text and use FromBase64() and Picture.FromString() on the other end.
The application is free to run as the picture finding stuff is all happening in the background.
the program only pauses when it receives and processes data from the task.
I added a Wait command to intentionally slow the background task down,
Posted
Enthusiast

BruceSteers said
okay then…
A simple Task example.
On load it just reads the HOME dir and gets filenames and icons in the background.
then the filename and icon picture are picked up by the Read event and added to a GridView.
I Save the picture to a temp file then load it with File.Load() then convert it to a Base64 String in the background task ,
then it's safe to Print as text and use FromBase64() and Picture.FromString() on the other end.
The application is free to run as the picture finding stuff is all happening in the background.
the program only pauses when it receives and processes data from the task.
I added a Wait command to intentionally slow the background task down,
Thank you for that
And please forgive my ignorance here but would this work in the following scenario
Cashier does a sale and scans 100 items and the sale completes and the background task does it things (renames the .temp files to the transaction number and starts processing them)
The application would then rest the system ready for another sale (while still processing the 100 items scanned)
Then the cashier would scan 3 items and completes the sale (so would another incident of the background task be created?)
Or do I need to keep an eye on the data folder and whenever I detect files in the folder get the file name and then kick off a Task?
Andy
Posted
Guru

but do not use the global $hDirTask variable, use a local one in the function
Posted
Guru

Posted
Enthusiast

BruceSteers said
If you passed the filename in the New statement then a separate task could be created for each file.
but do not use the global $hDirTask variable, use a local one in the function
Does this mean the code I want to use does not have to be all inside the task?
What I mean is inside the task can I call the global.fileRename function that I already use?
Posted
Guru

I thought that was the whole point of doing this?
Posted
Guru

AndyGable said
What I mean is inside the task can I call the global.fileRename function that I already use?
It's worth trying,
The Task is a fork
To quote the wiki…
wiki said
The Main method is run by a fork. It can access every other part of the program, except that the parent process will not see any change done by the task.
Many components will not like being forked. Especially the GUI ones.
Hopefully, Gambas tries to be as nice as possible with them in that case. But be careful anyway.
It might help to put this at the top of your Task class to make sure it sees the Global.class (but it might not be needed)
But it might cause the lag on your running program you are trying to avoid? I'm not sure how that works.
If it was me i would put all the Function code needed in the Task in the task so it's purely standalone,
Posted
Enthusiast

I assume it would just be a sub with in the class
A not like
Etc and if I do need to use the code else where in the program I can just copy the module and call it for example RenameFile_1 lol
Posted
Enthusiast

I have done some updates and it was working but now every time the application run and it comes to the background task I get this error message in the console. I am running 3.19.3
Posted
Guru

Posted
Enthusiast

BruceSteers said
Then I guess you broke it somehow.
Only I could break it
1 guest and 0 members have just viewed this.


