Threading library for optimising compiler

Discussion in 'Additional Libraries' started by agraham, Jan 18, 2008.

  1. agraham

    agraham Expert Licensed 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.basic4ppc.com/forum/addi...-threading-library-version-2-a.html#post27720
     

    Attached Files:

    Last edited: Oct 29, 2009
  2. dzt

    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:)
     
  3. agraham

    agraham Expert Licensed User

    Thanks dzt. I keep doing that - turning it off for testing then forgetting to turn it back on! Zip modified.
     
  4. agraham

    agraham Expert Licensed User

    Help now added to the original post. As well as documenting the library it includes a brief introduction to threads and processes.
     
  5. agraham

    agraham Expert Licensed 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.
     
  6. dzt

    dzt Active Member Licensed User

    :)

    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: Jan 23, 2008
  7. Cableguy

    Cableguy Expert Licensed 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)???
     
  8. agraham

    agraham Expert Licensed User

    Can I ask why? Actually this is not possible due to the nature of what a timer is. Tell me what you need and perhaps we can see how to achieve it.
     
  9. Cableguy

    Cableguy Expert Licensed 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?
     
  10. agraham

    agraham Expert Licensed 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.
     
  11. Cableguy

    Cableguy Expert Licensed 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?
     
  12. agraham

    agraham Expert Licensed User

    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.

    Your about panel seems to NOT halt the main code of a B4PPC app.
     
  13. Cableguy

    Cableguy Expert Licensed 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...
     
  14. agraham

    agraham Expert Licensed User

    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.
     
  15. Cableguy

    Cableguy Expert Licensed 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....
     
  16. agraham

    agraham Expert Licensed User

    Sorry - bad practice - NEVER rely on a user to do the right thing ;)
     
  17. Elrick

    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 :(
     

    Attached Files:

    • 11.sbp
      File size:
      892 bytes
      Views:
      10
  18. agraham

    agraham Expert Licensed User

    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.
     

    Attached Files:

    • 11.sbp
      File size:
      985 bytes
      Views:
      12
  19. Elrick

    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: Feb 29, 2008
  20. agraham

    agraham Expert Licensed 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.
     

    Attached Files:

    • 11.sbp
      File size:
      1.1 KB
      Views:
      19
    Last edited: Feb 29, 2008
Loading...