Hi Susu
I haven't finished my code, but so far I have done this. I am still a noob at all this stuff, but this might help you on the way.
My code makes use of the Sub InsertMaps from Erel's DB_UTILs code module. I took that out, put it into my Service code, and changed it as follows.
I added a couple of global variables into the Service code module.
Dim TotalNumberOfItems as Int
Dim ItemsSaved as Int
Then, after the start of the loop in the Sub InsertMaps section, (For i1 = 0 To ListOfMaps.Size - 1) I added a CallSub back to the calling activity which would update the UI since Services cannot interface with the UI. I call this a callback as you're "calling back" to the main activity.
So, if your calling Activity was Main, and your service was called DataService, the code in InsertMaps would look something like this:
TotalNumberOfItems = ListOfMaps.Size 'NEW CODE
For i1 = 0 To ListOfMaps.Size - 1) 'Erel's code
ItemsSaved=i1+1
CallSub(Main,"ShowProgressInfo") 'New code - this is the callback
..... 'rest of Erel's code
Next i1
The code you need in your Main Activity is something like this, assuming you're putting up a label and a progress bar.
Sub ShowProgressInfo 'Used by DataService for callback
labelProgress.Text = "Downloaded " & DataService.ItemsSaved & " of " & DataService.TotalNumberOfItems & " items."
progressbar1.progress = (DataService.ItemsSaved / DataService.TotalNumberOfItems)*100
End Sub
Note that you should make sure that the Main Activity has not been paused by using IsPaused(Main) to figure out whether the Main Activity will respond. My code does not show that side of things.
Doing the callback does slow things down a bit especially if you are downloading large numbers of items, but you could put in a counter before the callback to only call back every 7 or 20 or 30 or whatever 'x' number of items - you decide.
What's nice is that at least the user sees that something is going on, and can see the progress of the "download/save" operation while connecting to a web service, either through the phone network or through a wireless connection.
If you use the Service code from Erel, you can get it all working in the background, and when you get it working it truly is a good feeling. It sure was for me when I finally got it working without hogging the main thread as previously discussed!
Hope this helps a little bit, and if the theory is all wrong, I am prepared to see it all shot down in flames - it seems to work for me!
JMB