Download the free trial version
Basic4android Video
Features
Tutorials and manuals
Showcase
Screenshots

Go Back   Android Development Forum - Basic4android > Basic4ppc (Windows Mobile) > Questions (Windows Mobile)
Documentation Wiki Register Members List B4P Search Today's Posts Mark Forums Read

Questions (Windows Mobile) Post any question regarding Basic4ppc.

Using Asynchronous GetXmlHttpObject() vs threads

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-10-2008, 08:45 PM
Junior Member
 
Join Date: Jun 2008
Posts: 39
Default Using Asynchronous GetXmlHttpObject() vs threads

Hi All, I'm having trouble with my new application because it uses the http module to received data over the net.

Because it is a synchronous protocol, my whole program freezes up while it does this and it becomes problematic, interfering with what the user may be doing on the device, especially if the internet connection is of poor quality.

What I really need is a way of putting this process into the background, so that it doesn't interfere with the user doing other things.

Is there a way to do this with threads, or by using some of the new Ajax techniques?

Thanks, Adrian
Reply With Quote
  #2 (permalink)  
Old 07-11-2008, 07:47 AM
agraham's Avatar
Basic4ppc Expert
 
Join Date: Jul 2007
Location: Cheshire, UK
Posts: 6,072
Awards Showcase
Innovator medal Beta Tester Forum Contributer 
Total Awards: 3
Default

Try my Threading library http://www.basic4ppc.com/forum/addit...-compiler.html It provides threading for optimised compiled programs. Pitfalls are detailed in the help which is worth a detailed study. Particularly note that you musn't touch any GUI stuff in a Thread, that's why I provided Thread events.
Reply With Quote
  #3 (permalink)  
Old 07-11-2008, 03:19 PM
Junior Member
 
Join Date: Jun 2008
Posts: 39
Default

Thanks, that looks like the solution I've been hoping for, I'll definitely give it a whirl.

Just wondering, what do you think about using Ajax techniques as an alternative - is this even possible with b4ppc?

Thanks, Adrian
Reply With Quote
  #4 (permalink)  
Old 07-11-2008, 03:50 PM
agraham's Avatar
Basic4ppc Expert
 
Join Date: Jul 2007
Location: Cheshire, UK
Posts: 6,072
Awards Showcase
Innovator medal Beta Tester Forum Contributer 
Total Awards: 3
Default

As I understand it, and I'm not too good on Webby stuff, Ajax is a web server/browser interaction that requires client side scripting. I could give a long complicated answer but the short answer is it is neither appropriate for nor possible with B4ppc.

As a matter of background B4ppc is single threaded and historically byte-code interpreted. More recently it has grown a fully compiled capability. It deliberately does not provide asynchronous methods in it's libraries as this would be complicated to implement in a byte-code machine and a complication for the programmer in it's role as a simplified rapid development environment.

My Threading library, which only actually threads on optimised compiled apps, is one of my subversive attempts to make things more complicated by letting the synchronous operations of B4ppc effectively become asynchronous. It allows asynchronism without getting into the complications of setting callbacks that .NET fully asynchronous operations demand and which are not supported by B4ppc.
Reply With Quote
  #5 (permalink)  
Old 07-11-2008, 04:20 PM
Junior Member
 
Join Date: Jun 2008
Posts: 39
Default

Thanks! That's a really good answer, imho, just what I was trying to understand ... onward ho to the thread library it is ...

Regards, Adrian
Reply With Quote
  #6 (permalink)  
Old 08-18-2008, 02:37 PM
Junior Member
 
Join Date: Jun 2008
Posts: 39
Default

Hi Andrew, I tested your excellent thread library with the 'ThreadTest.sbp' example you provided.

The thread that displayed the counter 0-1000 seemed to work well, so I then replaced just that section with an http page call. I then disabled my net connection and found that it behaved the same as ever and locked up the program until the timeout completed, then it just stopped, ignoring the ErrorLabel.

Any idea why that wouldn't work?

Tks, Adrian
Reply With Quote
  #7 (permalink)  
Old 08-18-2008, 02:49 PM
agraham's Avatar
Basic4ppc Expert
 
Join Date: Jul 2007
Location: Cheshire, UK
Posts: 6,072
Awards Showcase
Innovator medal Beta Tester Forum Contributer 
Total Awards: 3
Default

Quote:
Originally Posted by aerohost View Post
Any idea why that wouldn't work?
As long as you had optimised compiled it then no! Can you post the code you tried?
Reply With Quote
  #8 (permalink)  
Old 08-18-2008, 03:40 PM
Junior Member
 
Join Date: Jun 2008
Posts: 39
Default

Hi Andrew, thanks for your reply. I put the changed sub at the bottom, nothing else was altered.

So I think the clue here is your 'optimised compiled' statement. I am only testing it from the IDE, without making an exe. Is that creating problems, in other words, do I have to make an exe to test it properly?

Also, I'm confused by the 'legacy mode' message box I keep getting when run from the IDE. Does that mean I don't have all the required up-to-date stuff (I thought I had the latest versions of everything), or do I just get that because I'm running from the IDE?

Thanks, Adrian
---------------------
Sub ThreadCode
ErrorLabel(ThreadCodeErr1) ' if abort is used to stop a thread an (intended by .NET) error occurs

'try to fetch data from server
Response.New1
Request.New1("http://myURL")
' Msgbox(url)
'This line calls the server and gets the response.
Response.Value = Request.GetResponse

string = Response.GetString 'Get the Response string.
'did we get data? if we hit next line, we did
Msgbox(string)
tb1text = "ok"

' For i = 1 To 1000
' For j = 1 To 1000
' k = i + j
' Next
' tb1text = i
' Thread.FireThreadEvent ' events work for testing even if not running on a separate thread
' Next
ThreadCodeErr1: ' ignore any error
tb1text = "not ok"
'Return k ' stops unused variable error
End Sub
Reply With Quote
  #9 (permalink)  
Old 08-18-2008, 03:51 PM
agraham's Avatar
Basic4ppc Expert
 
Join Date: Jul 2007
Location: Cheshire, UK
Posts: 6,072
Awards Showcase
Innovator medal Beta Tester Forum Contributer 
Total Awards: 3
Default

Quote:
Originally Posted by aerohost View Post
Is that creating problems, in other words, do I have to make an exe to test it properly?
The second paragraph of Overview in the help
Quote:
The Thread object of this library is intended for use with the optimising compiler of Basic4PPC version 6 onwards. However it can run, after a fashion, in the IDE and in a legacy compiled application. The library protects itself, and you, if it finds itself running in a legacy mode and issues warnings that functions are not available. The purpose of allowing this is to enable debugging of thread code in the IDE because debugging a compiled application is more tricky than using the debugger in the IDE.
Threading does not work in the IDE as that is a byte-code environment (known as legacy mode to us cognoscenti ) as is non-optimised compilation. True threading will only work in an optimised compiled app. However you can debug a threaded app (sort of) in the IDE and if you read the demo app closely it has some pointers in there as to how. To debug an optimised compiled app you can try the Watcher from my debug library which can look inside a running optimised compiled app. http://www.basic4ppc.com/forum/addit...g-library.html

Quote:
Also, I'm confused by the 'legacy mode' message box I keep getting when run from the IDE. Does that mean I don't have all the required up-to-date stuff (I thought I had the latest versions of everything), or do I just get that because I'm running from the IDE?
That's the library warning you that threading is not available in the legacy environment.

Last edited by agraham : 08-18-2008 at 03:59 PM.
Reply With Quote
  #10 (permalink)  
Old 08-19-2008, 05:07 PM
Junior Member
 
Join Date: Jun 2008
Posts: 39
Default

Hi Andrew, ok, thanks very much for your feedback. I'm using your 'ThreadTest.sbp' example to try and understand how this works.

In the 'Sub ThreadCode' you have the loop that counts to 1,000 you have this code:

For i = 1 To 1000
For j = 1 To 1000
k = i + j
Next
tb1text = i
Thread.FireThreadEvent
Next

I'm presuming this is the actual background task you are running in a thread to demonstrate that the user can still do other things on the form while the text box displays the updated value. This works fine for me - it's a good way to demonstrate events running in a background thread.

So, I'm trying to substitute that code with a simple http request, as follows:

Response.New1
Request.New1("myURL")

'This line calls the server and gets the response.
Response.Value = Request.GetResponse
Thread.FireThreadEvent

If I'm connected to the net, it works fine, but when I disconnect from the net, the whole form is stalled and you can't do anything else. I have put a drop-down listbox on the form as an example of doing something on the form while the internet is being accessed, and it is frozen while the thread attempts to connect to the non-existent internet connection.

This is the essence of my problem: I need the form activity to continue, when the http request is made, even if the internet connection is broken.

I'm wondering if I have the 'Thread.FireThreadEvent' statement in the wrong place?

I haven't changed anything else, except in the initialization sub:

#Region Create and start a thread

Thread.New1(B4PObject(1))
If Optimising Then
Thread.Start("ThreadCode") ' returns true if started, false if not
Else
'ThreadCode
End If

#End Region

I commented out the ThreadCode line so nothing would happen until I pressed the start button because the legacy message was getting in the way, and I needed to be able to clear that first to test it properly?

So can you see any reason why the http request jams up everything?

Thanks, Adrian
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
New feature: Similar Threads Erel Forum Discussion 0 01-23-2008 02:15 PM


All times are GMT. The time now is 10:03 AM.


Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0