Android Question Help writing clean code for background processes

MrKim

Well-Known Member
Licensed User
Longtime User
I am a long time VBA programmer and used to doing things a certain way. I don't mind change but I want to do it right.

I am using the SMB lib to copy files to a Window network (wonderful - works like a charm) my problem is I can't for the life of me see how to write clean, readable code with it. I feel like I must be missing something obvious. SMB runs in the background and fires an event when it is done. I am used to something like this (Pseudocode):

B4X:
Sub MySubCopyFile
 
  On Error Go To MyErr
 
  CopyFile LocalBlah, Blah
 
  CopyFile LocalBlah2, Blah2
 
  CopyFile LocaBlah3, Blah3
 
 
  DeleteFile LocaBlah
 
  DeleteFile LocaBlah2
 
  DeleteFile LocaBlah3
 
 
Exit Sub
 
MyErr:
 
Do Something About The Error
 
End Sub

If anything fails it jumps to the error routine and the rest of the code does not run.

With SMB if I do this:
B4X:
Sub MySub
 
  UploadFile LocalBlah Blah
  UploadFile LocalBlah2 Blah
  UploadFile LocalBlah3 Blah
 
  DeleteFile LocalLocaBlah
  DeleteFile LocalLocaBlah2
  DeleteFile LocalLocaBlah3
End Sub
 
Sub UploadCompleted Blah
  If Failure
     Do Something
  Else
     Do Something Else
  End If
End Sub

It will run all of the code, delete my local files without waiting to see if they copied successfully.
I tried putting a DO Doevents WHILE loop (with a timeout of course) after the upload to wait for Upload Completed but UploadCompleted Does not fire as long as the doevents Loop is running.

I could of course do a variable flag and test it in UploadCompleted to see where I am at and act accordingly but this becomes ugly, unreadable code and what happens when you call another Upload from inside the return event for the previous upload?

I feel like I must be missing something obvious here.

I love B4A and I am awed by this developer community! Every one here is amazing. Thank you all and thanks in advance for any help.

Kim
 
Last edited:

MrKim

Well-Known Member
Licensed User
Longtime User
Forgive this old dinosaur but could somebody give me an example? Or point me to an example? Are you telling me I should use, for example a list of subroutines to call and delete them from the list as they are done? Sample code is worth the proverbial thousand words to me. I am really used to just writing good, old linear VBA code. Events only when I am waiting for a user response. I am loving the heck out of learning this stuff but it is very foreign to me.
What I need to do is
upload a file to the network
verify that the file uploaded correctly - if it has then
wait for a response file
download the file
verify the data and act on it if it is valid.

PS: From a programming philosophy standpoint this seems like it is writing code that is totally unreadable, at best very hard to follow as it forces you to jump all over the place. Again, I feel like I am missing something. What should take a few lines of code in one sub winds up wandering all over the pl

Thanks for your help.

Kim
 
Upvote 0

Eric H

Active Member
Licensed User
Longtime User
IMO, writing useful and readable code is about writing code that is both extendable, and modular. Creating methods that can be fed some info and return a result is much more useful, modular and extendable (even though it looks like it is jumping all over the place) than writing a bunch of individual lines of code that are specific only to that one task and can't be useful beyond that. What happens if you need to manage 100 times the amount of data? With modular code it is easy. With uniquely coded specific lines of code, on the other hand, its going to wear out your copy/paste buttons.

Just my $0.02. :)

Cheers,
Eric H
 
Upvote 0

MrKim

Well-Known Member
Licensed User
Longtime User
Eric,
Well, that is the part I am trying to grasp. What I wanted to write was a single sub that I could pass a set of parameters and when it was done I would know for sure that I had uploaded the file I was trying to upload. I have different files I want to upload and what happens AFTER the upload will vary. I know the files will be small, I am managing them from the tablet.

The downloads are a different story, they might be large and take some time. I will handle those differently.

The point is I am looking for a good, clean way to upload a file and wait for the upload to return Success or failure so I can proceed. At present the only way I can see to do this is either with multiple SMB objects or reinitialize the same one
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Handling asynchronous events is more difficult than working with synchronous methods. However the advantage is that your program is always responsive. In fact Android shows an "application not responding" dialog if the main thread is occupied for more than a few seconds.

Note that if the Success parameter is true then the file was uploaded or downloaded successfully.

There are many ways that you can use to manage the program state. I recommend you to implement it as a service instead of an activity. Services are simpler to work with as they are not paused.

There is definitely no reason to use more than one SMB object.

You can create a Class (or Type) that represents a task and then use a Map to map between the URL and the class/type. In each event you will first fetch the object from this map and then do whatever you need to do with the information you have. If you implement it as a class you will be able to call:
B4X:
Sub DownloadCompleted(...)
 Dim MyTask1 As MyTask = MapOfTasks.Get(Url)
 MyTask1.AfterDownload(Success)
End Sub
 
Upvote 0
Top