Threading library for optimising compiler

Elrick

Member
Licensed User
I apologise for my poor descriptions, i'm just bad english-speaker, so i'm trying to write as short as possible messages to make as less as possible mistakes :) Yes this code works without an errors, but it works from the second-third time... When i press the button for the first time, there is always "no response received" inscription...
 
Last edited:

agraham

Expert
Licensed User
Yes this code works without an errors,
Good. You need to keep that error trapping in place.

but it works from the second-third time... When i press the button for the first time, there is always "no response received" inscription...
That cannot be anything to do with the threading library. More likely it is something in your device IP stack that causes the initial delay. Try increasing your HTTP timeout.
 

Elrick

Member
Licensed User
omg! I have written timeout 3000 instead of 30000! This is the main error :) Now all works fine, thank you very much, agraham, for this fantastic library and for you help in answering to my stupid questions :)
 

TWELVE

Active Member
Licensed User
Hello agraham,

i tested a bit with your threading lib.I just call a sub as a thread, which appears to work fine.But i'm missing some responses from my sub.All my code parts are writing messages in a console.This is used as a status display and also for debugging purposes.The code is just calling a sub, which does log the message to the console control and also to a log file.The "console printing" sub is using a textbox control for the output, which is under the control of the main gui program.

Questions:

- what's wrong here ?
- Can i call a sub from a sub which has been started as a thread or not ?
- if it's ok to call a sub from a threaded sub, can the called sub access the controls of the main gui program ?
- if not, how can i work around this ?



cheers

TWELVE
 

agraham

Expert
Licensed User
- what's wrong here ?
I don't know!
Can i call a sub from a sub which has been started as a thread or not ?
Yes, there should be no problem unless you also call that Sub from the main program or another thread. Keep thread code strictly segregated from other code.
if it's ok to call a sub from a threaded sub, can the called sub access the controls of the main gui program ?
No. Please read carefully the Thread Pitfalls page of the help. Especially the GUI operations section.
if not, how can i work around this ?
Use FireThreadEvent or FireDebugEvent to "call" your subs that manipulate GUI controls. They are provided as a safe path to the the GUI thread. You can only safely manipulate GUI controls in the main thread or in these events or Subs called by them. If you need multiple different operations then distinguish them in the event by setting a Global variable in the thread and doing a Select.... Case in the event.
 

TWELVE

Active Member
Licensed User
Hi agraham,

thx for your reply.

I read the threading lib manual carefully, also the pitfalls section, but this was only talking about it would be unsafe to manipulate GUI elements, not it would be impossible.So for me it was not clear that i don't even have access to the GUI controls.

I tested with the Thread_ThreadEvent, which worked fineso far.However, i need to rework the code to have it communicating with the GUI.Furthermore the desktop simulation is throwing two error messages per thread call:

Library running in legacy mode.
Start, RunLock and Join are unavailable.

Start
Not available in legacy mode.

That is really impacting me because it renders the desktop emu nearly useless, unless i confirm 2 error boxes every second or so - depending on how often the thread is being called.

What is the reason for these errors ? Has the desktop lib/version only limited functionality ? Is it possible to suppress these error messages..?



kind regards

TWELVE
 

agraham

Expert
Licensed User
would be unsafe to manipulate GUI elements, not it would be impossible.
Unsafe means don't do it because bad things can happen!


What is the reason for these errors ? Has the desktop lib/version only limited functionality ? Is it possible to suppress these error messages..?
They are not errors. This is explained this in the the "Thread debugging" section of the help. Please re-read it. As the first line says "Threads may only be used in optimised compiled Basic4PPC applications". That section of help documents the differences between optimised and legacy operations and the demo app shows how to ameliorate some of those differences.
 

mattsaintdev

Member
Licensed User
Dependencies not found error

Hi, im just having a go with the threads lib 1.3 and compiled and downloaded to the PDA. I get an error which states;

"Threading version 1.0.3179... culture=neutral. publickeytoken=null, one or more of its dependencies , was not found."

Ive updated the .NET Cf to 3.5 recently (to get basic4ppc working as the 2.0 was out of date) so ive no idea what dependencies would not be found.

Any help greatly appreciated.
 

derez

Expert
Licensed User
using a thread for network

Hi Agraham.
In my CHAT program I connect two devices (or desktop and device). The server listens until one client connects and they start communicating.
I would like to enable more than one client to join.
do you think that running the server.accept command in a thread will enable it ?
Anyway - look at the code below and please advise me how to do it.
The original code goes like this:
B4X:
sub serverB_click
waitforconnection.Enabled = True
server.New1(50000) 'Listens on port 50000 (all ip's available).
server.Start
End Sub

Sub WaitForConnection_Tick
If server.Pending = True Then
   client.Value = server.Accept 
   remote_on_flag = 1        ' communication established
   WaitForconnection.Enabled = False  ' this is where I have to stop the timer
   WaitForData.Enabled = True
End If
End sub
 
Last edited:

derez

Expert
Licensed User
Well I guess I succeeded to do something - I can connect two but the first is not connected after the second does.
I think that is something which is not related to the threading.
Thank you for this library, I'm going to use it :)

B4X:
Sub WaitForConnection_Tick
If Not(Thread.Start("Main.accept")) Then accept 
If accepted = 1 Then
   remote_on_flag = 1        ' communication established
   'WaitForconnection.Enabled = False
   WaitForData.Enabled = True
       accepted = 0
End If
End Sub

Public Sub accept
If server.Pending = True Then
   client.Value = server.Accept 
   accepted = 1
End If
End Sub
 

agraham

Expert
Licensed User
B4X:
Sub WaitForConnection_Tick
If Not(Thread.Start("Main.accept")) Then accept
This looks odd. If you can't start Sub accept as a thread you call it directly. However the probable reason for it not starting is that it is running already! Trying to run the same code on two different threads at the same time often spells trouble. Is there a reason why you need to do this?

Also you can't connect a local client to more than one remote client at one time. To service multiple remote clients you need multiple local clients to receive the clients created by Server.Accept.

If you want to close and then reopen a local client to deal with another remote client note that, unfortunately, Client.Close doesn't close the underlying Stream. You can do this with the Door library - see this thread http://www.basic4ppc.com/forum/questions-help-needed/3298-connection-not-closed.html
 

mattsaintdev

Member
Licensed User
Thread Locks ?

Hi, im using the thread lib to greatly improve my app by putting the HTTP.get's in the background - which stops the application jerking as i used timers in the first instance.

However, my question is that i cant see anything to create a criticalsection around shared variables etc. Ive used sleeps to effect a spinlock but its not elegant - any chance of adding semaphores or critical sections please?

Thanks
 

agraham

Expert
Licensed User
i cant see anything to create a criticalsection around shared variables etc.
That's what RunLockedx is for. Stick the accesses you need protected in a Sub and run it by calling the appropriate RunLockedx method from both your thread and your main code. A Sub run by RunLockedx is run as a critical section that can only be entered by one thread at a time. The call blocks until the synchronisation object is free. It uses the C# "lock" construct. If you look at the source it's like this (almost!)

B4X:
        public string RunLocked0(string sub)
        {
            // minimise locked time by looking up the Sub delegate first
            b4p.del0 del = (b4p.del0)htSubs[b4pname(sub)];
            lock (this) // use the Basic4ppc Thread object for synchronisation
            {
                return del(); // del is the delegate that invokes the Sub
            }
        }
See the section "Thread safety" in the "Thread pitfalls" topic of the help.
 
Last edited:

agraham

Expert
Licensed User
Version 1.4 of the Thread library now posted. Thread was changed to now try to check the existence of a Sub specified by RunLockedX or Start before using it. This might help prevent puzzling behaviour in the case of typos in your source code.

Process has a Responding property and a WaitForExit method added. Yield has been corrected. Because of a cut and paste error copying the code from Sleep it previously needed a time parameter but didn't actually use it. Now it doesn't take a parameter.

The ThreadTestDevice demo has always needed my speech library here http://www.basic4ppc.com/forum/additional-libraries/1250-speech-library-device.html#post6582 but I have failed to note this in the past. I can't include it as it causes the size of the zip to exceed the forum upload limit.
 
Last edited:

TWELVE

Active Member
Licensed User
Hello agraham,

i upgraded to B4P 6.50 from 6.30 recently, on this occasion i updated the threading lib from 1.2 to 1.4, which appears to cause errors in my program now.When my thread is called, i get an "unexpected error".In the details i find this here:

NullReferenceException at Threading.Thread.wrapper()

I went back to 1.2, but the symptom remained unchanged.So i has likely nothing to do with your lib, but it is the point where the symptom occurs.

I already regret to have upgraded to 6.50, because instead of working further on my project i'm spending time now on chasing errors and exceptions i did not have before...:confused:

Unfortunately i saved my last source under 6.50, it seems to apply changes to the source code, so i cannot roll back to 6.30 now.

Any suggestions..?


regards,

TWELVE
 
Top