Background task with in application - is this possible?

Post

Posted
Rating:
#1 (In Topic #1282)
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’
 Hi everyone,

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
Online now: No Back to the top

Post

Posted
Rating:
#2
Online now: No Back to the top

Post

Posted
Rating:
#3
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’

BruceSteers said



BruceSteers

Yea I've been reading up on that but there is no code Example as how to use it.
Online now: No Back to the top

Post

Posted
Rating:
#4
Guru
BruceSteers is in the usergroup ‘Guru’
Well it explains it on the wiki.  there's not much to it.
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)
Online now: No Back to the top

Post

Posted
Rating:
#5
Avatar
Regular
thatbruce is in the usergroup ‘Regular’
I think there is some stuff in the Book too, somewhere. https://www.gambas-buch.de/doku.php?id=start

Online now: No Back to the top

Post

Posted
Rating:
#6
Guru
BruceSteers is in the usergroup ‘Guru’
 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,

Attachment
Online now: No Back to the top

Post

Posted
Rating:
#7
Enthusiast
AndyGable is in the usergroup ‘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 BruceSteers

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
Online now: No Back to the top

Post

Posted
Rating:
#8
Guru
BruceSteers is in the usergroup ‘Guru’
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

Code (gambas)

  1.  
  2. Public Sub ProcessSale(sFileName)
  3.  
  4. '  do local stuff
  5. DoLocalSaleStuff(sFileName)
  6.  
  7. ' then start a task  
  8.   Dim hSaleTask As ProcessSaleTask = New ProcessSaleTask(sFileName)
  9.  
  10.  
  11.  
Online now: No Back to the top

Post

Posted
Rating:
#9
Guru
BruceSteers is in the usergroup ‘Guru’
Try it and see, let us know if you get stuck
Online now: No Back to the top

Post

Posted
Rating:
#10
Enthusiast
AndyGable is in the usergroup ‘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

Code (gambas)

  1.  
  2. Public Sub ProcessSale(sFileName)
  3.  
  4. '  do local stuff
  5. DoLocalSaleStuff(sFileName)
  6.  
  7. ' then start a task  
  8.   Dim hSaleTask As ProcessSaleTask = New ProcessSaleTask(sFileName)
  9.  
  10.  
  11.  

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?
Online now: No Back to the top

Post

Posted
Rating:
#11
Guru
BruceSteers is in the usergroup ‘Guru’
 The more code you put in the task the less your programs reported bottle necking will be.

I thought that was the whole point of doing this?
Online now: No Back to the top

Post

Posted
Rating:
#12
Guru
BruceSteers is in the usergroup ‘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)

Code (gambas)

  1. Class Global
  2.  

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,
Online now: No Back to the top

Post

Posted
Rating:
#13
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’
Well as most of the code that is called by the task I want to use is no used by any other part of the system I could just move it into the task

I assume it would just be a sub with in the class

A not like

Code (gambas)

  1. Class endofsaletasks
  2.  
  3. Sub main
  4.  
  5.  
  6. Sub renamefiles
  7.  

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
Online now: No Back to the top

Post

Posted
Rating:
#14
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’
Hi All,

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

Code (gambas)

  1. [xcb] Unknown sequence number while processing queue
  2. [xcb] You called XInitThreads, this is not your fault
  3. [xcb] Aborting, sorry about that.
  4. gbx3: ../../src/xcb_io.c:278: poll_for_event: Assertion `!xcb_xlib_threads_sequence_lost' failed.
Online now: No Back to the top

Post

Posted
Rating:
#15
Guru
BruceSteers is in the usergroup ‘Guru’
Then I guess you broke it somehow.
Online now: No Back to the top

Post

Posted
Rating:
#16
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’

BruceSteers said

Then I guess you broke it somehow.

Only I could break it  :lol:
Online now: No Back to the top
1 guest and 0 members have just viewed this.