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?
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.
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.
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.
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
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.
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?