Threading library for optimising compiler

agraham

Expert
Licensed User
Longtime User
The introduction of the true compiler for Basic4PPC opens up some avenues previously closed. One of these is the ability to write threads in Basic4PPC code rather than only being able to do this in a library.

Some/most of you will know that a thread is a separate flow of program execution. Basic4PPC normally has only one thread, the GUI (Graphical User Interface) thread that, not surprisingly, handles the user interface (and everything else). Using this library you can create additional threads that operate (apparently) in parallel alongside each other and the GUI thread.

Threads can be useful to keep the user interface alive while performing a long sequence of computations. For instance my Flite speech dll for the device runs on the GUI thread and while it is speaking you can't do anything else. Included is an example of running it on a thread so you can set it off and leave it to run and carry on user interaction. You need FliteDevice.dll and fliteDLL.dll in the app folder on the device.
Speech library for the DEVICE!
Compile ThreadTestDevice.sbp optimised for device with FliteDevice.dll and Threading.dll in the desktop app folder and transfer the resulting exe and Threading.dll to the device app folder.

The library protects itself if it finds itself running in a legacy mode so hopefully you can't crash your app whilst playing! I'll produce a help with more info later. However there is a load of comment in the ThreadTest app to hopefully give you enough information to try it out - should you dare!

EDIT :-I've updated demo source as per dzt comment below. Also I just noticed a minor issue in my workarounds to issue warnings when running in legacy mode (where it doesn't work anyway). I don't quite understand the problem at the moment :confused: but it doesn't really matter as it is cosmetic really. Just shows you can't do enough testing before release :(

EDIT :- Update to version 1.1 to warn politely (as always intended) rather than throw an error when any of the RunLocked calls were used in legacy mode.

EDIT : - Updated to 1.2 with Process object added.

EDIT : - Updated to 1.3 with support for module name prefixes for the thread Sub. Help and source included. Demo sources are in 6.50 format but the library is compatible with all Basic4ppc 6.xx releases.

EDIT : - Updated to 1.4. See post #39 for details.

EDIT : - Updated to 1.5 for a bug fix. See post #46 for details

THIS LIBRARY IS REPLACED BY http://www.b4x.com/forum/additional-libraries/4805-threading-library-version-2-a.html#post27720
 

Attachments

  • Threading1.5.zip
    33.8 KB · Views: 113
Last edited:

dzt

Active Member
Licensed User
Excelent!!!

A lot of knowledge is hidden here. And of course a lot of work

PS.
You should immediately turn "Check for unassigned / unused variables" on.
Try to run ThreadTest.sbp in debug mode:)
 

agraham

Expert
Licensed User
Longtime User
Library version 1.2 and updated help posted to original post. This version changes the name of the "Threader "object to "Thread" as I didn't like the original name and it's my library and I can do what want with it :) Otherwise there are no changes to the Thread object.

Added is a Process object which provides the facility that several people have asked for in the past. This is the ability to start a program (process) and know when it has finished. The Shell statement in B4PPC starts a program (process) but that is all. The Process object here can start a program, provide an Event when it terminates and can forcibly terminate it if required.
 

dzt

Active Member
Licensed User
...This version changes the name of the "Threader "object to "Thread" as I didn't like the original name and it's my library and I can do what want with it :) ...
:)

I feel the need to say again how useful is this library, as there are many b4ppc functions taking long time to finish (FTP, Image processing etc.) and until then it is not so elegant to have the UI frozen.

I also should say that I love Process object.
 
Last edited:

Cableguy

Expert
Licensed User
Longtime User
I'm about to use this lib for the first time, so before i do i need to ask just one thing...
I need to thread a timer...
is this thread fired as a single sub? (timer1_tick)???
 

Cableguy

Expert
Licensed User
Longtime User
I am writing an "alert" progam, in wich the user will enter a series of data including a date field...
in the mean time the program ( i have thought of an "external"program...), will, twice a day, as setted by the user, check if the date entered above matches the Today date, and if true, pop-up an Alert....But it must not halt the execution...
So what the best way to go?
 

agraham

Expert
Licensed User
Longtime User
Two possibilities spring to mind.

For both possibilities I would suggest using a timer with a fairly long interval to check whether it is the right time to start one of these checks then :-

a) Start another timer or reset the original timer's interval to be shorter, say 500mS or so. Do the data checks in the timer code but structure the code so that it, for example, checks one set of data, saves where it has got to in one or more global variables, then exits the timer picking up the data to check the next set in the next timer tick, saving and exiting again, and so on till complete.

b) Use the timer code to start a separate thread that does the checking in a long code loop without returning. If you want to display the alerts from within that code you would need to use a timer event as shown in the sample app. Alternatively you could build some global variables with the alert data and then set a global flag when the thread completes that the timer could check for and then display whatever is needed.
 

Cableguy

Expert
Licensed User
Longtime User
I think I will go with a separate executable handling the datafile and cheking the dates....
As I understand, and please confirm this, if I use something like my about panel as a pop-up, the code will NOT be halted...as it hapens qith a message box....Correct?
 

agraham

Expert
Licensed User
Longtime User
I think I will go with a separate executable handling the datafile and cheking the dates....
In that case you might want to use the Process object in the threading library, rather than B4PPC "Shell", as Process can tell you when the separate app terminates and you could then pick up the results from a file.

As I understand, and please confirm this, if I use something like my about panel as a pop-up, the code will NOT be halted...as it hapens qith a message box....Correct?
Your about panel seems to NOT halt the main code of a B4PPC app.
 

Cableguy

Expert
Licensed User
Longtime User
Thanks Agraham...
All good news, but I do not need to do a process separation, because the checking is simple...then I just take the data and have it pop-up...
It will be a "silent" app, as it will be minimized and only show in the taskbar...
Thanks for all your input...
If you'd like i can post the code as far as I have it, but it is in portuguese language, but fairly simple to understand..
I plan to have the "Main" app ready tonight...
 

agraham

Expert
Licensed User
Longtime User
It will be a "silent" app, as it will be minimized and only show in the taskbar
I assum that it will use the same data file as the main app. You will need to check what happens if this silent app tries to open the file at the same time as the main app (and vice versa) trap any errors and account for that in the code of both apps.
 

Cableguy

Expert
Licensed User
Longtime User
I think I have it covered, as I use the Loadcsv method of the table control, so the file lock should be kept to a minimum, and since the silent app will be running at a known time, the user will know when NOT to husle the Main app...
Still I will post my app so that, if you be so kind, take a look and share with me your thoughts....
 

Elrick

Member
Licensed User
Hi, agraham! It seems that your library is not work with HTTP library...
On the device, attached code raises an error :(
 

Attachments

  • 11.sbp
    892 bytes · Views: 10

agraham

Expert
Licensed User
Longtime User
Hi, agraham! It seems that your library is not work with HTTP library...
On the device, attached code raises an error :(
Please read the help, Thread pitfalls -> GUI operations, and look at the sample app. You cannot access GUI controls from a thread without causing an error. This is why I provided thread events. See this mod to your original code which now works.
 

Attachments

  • 11.sbp
    985 bytes · Views: 12

Elrick

Member
Licensed User
Thanks for you reply, agraham, but in my Glofiish M700 your code still causing an error:sign0148:

UPDATE: Hmm... This error calls "NotSupportedException" :) and if i ignore this error (choose to continue), the html code appears in TextBox...
 
Last edited:

agraham

Expert
Licensed User
Longtime User
I assume you mean that an error occurs when you run the modified 11.sbp code I posted. Next time please be more exact in describing exactly what you see happens rather than just saying an error occurred.

There is no error trapping in the thread in that code. In production code a thread subroutine should always have an errorlabel statement to trap errors otherwise B4PPC error handling will try to put up a message box from the thread which will itself cause an error as it is running on the threads' thread, not the GUI thread. Please try the attached code which should run without error in all situations - at least it does on my device.
 

Attachments

  • 11.sbp
    1.1 KB · Views: 19
Last edited:
Top