B4J Question Delete File after viewing it

aaronk

Well-Known Member
Licensed User
Longtime User
Hi,

I am using the server feature in B4J and I am using the following as an example:

B4X:
Sub Process_Globals
   Private srvr As Server
End Sub

Sub AppStart (Args() As String)
   srvr.Initialize("srvr")
   srvr.Port = 8888
   srvr.StaticFilesFolder = File.Combine(File.DirApp, "www")
   srvr.AddHandler("/hello", "HelloPage", False)
   srvr.AddHandler("/FormExampleHelper", "FormExampleHelper", False)
   srvr.AddHandler("/FileUpload", "FileUpload", False)
   srvr.Start
   StartMessageLoop
End Sub

Lets say there is a file named bob.xml in the www directory.

When a user views this file http://127.0.0.1:8888/bob.xml is there a way to then delete it? (even if it deletes the file 5 minutes later?)
 

Roycefer

Well-Known Member
Licensed User
Longtime User
Add a Filter to your server. It will know when a static page has been visited and then can start a Timer that deletes the static file 5 minutes later. You can also use CallSubPlus instead of a Timer.
 
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
Add a Filter to your server. It will know when a static page has been visited and then can start a Timer that deletes the static file 5 minutes later. You can also use CallSubPlus instead of a Timer.
I ended up using CallSubPlus.. it works perfect. thanks for your help
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
For this solution to work in release mode you need to use CallSubDelayed to call a sub in a code module with the file name. In the code module sub use CallSubPlus to delete the file after a few minutes.

You will not see this issue in debug mode as all filters and handlers are executed by the main thread. However in release mode each handler is handled by a different thread and by the time CallSubPlus fires, the relevant thread will no longer be available.

By calling the code module with CallSubDelayed you are delegating the task to the main thread.
 
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
If my understanding of B4J servers is accurate (and please correct me if it's not), using CallSubPlus from a Filter that has been added with SingleThreaded=True should work just fine (in release or debug mode). A single-threaded Filter runs in the main thread. Any CallSubPlus task spawned from that Filter will also run in the main thread.
 
Upvote 0
Top