Threading library version 2

Discussion in 'Additional Libraries' started by agraham, Aug 31, 2009.

  1. agraham

    agraham Expert Licensed User

    As this is a significant rework of the original Threading library I have posted it in a new thread. The significant difference is that this version will operate in the IDE as well as when optimised compiled! That's right - you can now test threaded applications in the IDE - desktop and device! It seems to work fine in the IDE although obviously performance is not as good as when optimised compiled. I think it is pretty stable, I haven't experienced any crashes so far in testing so I think it is ready for the wider world.

    The provision for locking resources has changed significantly. The previous RunLocked methods have been removed as they were inefficient to implement under the IDE. Instead two "proper" locking objects that are thin wrappers over two of the .NET locking objects are provided.

    Library, help, source and demos in the zip. As before ThreadTestDevice.sbp needs my

    EDIT :- Version 3.0 now posted for use with Basic4ppc v6.90 or later. See post #13 for details.

    Attached Files:

    Last edited: Apr 18, 2010
  2. Erel

    Erel Administrator Staff Member Licensed User

    This is amazing!
    I didn't think that it is possible to add this support without some major modifications to the internal code.
    Thank you once again.
  3. agraham

    agraham Expert Licensed User

    Thanks Erel :). I figured out that if the IDE code is re-entrant enough to cope with calling StartSub for Events and CallSub, which can occur in expressions, it was probably OK for Threading. Seems OK so far! It's a spin-off from trying to get a full Debug suite for the device IDE - specifically the Caller code.

    EDIT: I really meant DoEvents not Events!
    Last edited: Sep 1, 2009
  4. Byak@

    Byak@ Active Member Licensed User

    and for this lib too :-[ (I'm say about adding a dispose method)
  5. agraham

    agraham Expert Licensed User

    Same reply!

    TWELVE Active Member Licensed User

    Hi Andrew,

    first of all, thanks again for your great contribution..!

    After some time went by i'm working on my piece of code where i embedded your threading lib a long time ago.It works great so far,currently having an issue with the thread is getting hung / crashing sometimes, but this is not subject of my post - not yet.

    I intend to add more functions to my code, thus i need to check if a thread is still running.From the help file i found, that thread.running would be the property i have to check for, but unfortunately i could not figure out how to get this done.Likely, i'm facing the old problem : i do not think as much .NET as much it would be needed for this..*lol*

    Basically, from my logic it would need to be done as following:

    MyThread = CreateThread("MySub")
    If Thread.Running(MyThread) then ...

    This is only an example, i know that the correct syntax is actually different from this.It is more PureBasic style, which i am using often for the pc as well.

    This is a simplified version of my code in B4P:

    Thread.Start ( mySub)
    End Sub

    do something
    end Sub

    is the thread MySub still running ?
    thread.running does not work, apparently the sub does not know about a thread object


    So how do i tell the thread.running what thread i am talking about...? Is it possible to use that property from a sub that was not the originator of the thread at all..?

    I touch my B4P code first time after a quite long time i used other languages, so i apologize in advance if i overlooked something pretty easy...;-)


  7. agraham

    agraham Expert Licensed User

    Try Mythread.Running! Properties and methods need to be called on an object instance.

    TWELVE Active Member Licensed User

    Sorry...still not understood.Can you explain that more in detail..? How do i reference to a thread object i created in a different sub..? When did i a give name to my thread object...?



    TWELVE Active Member Licensed User

    I got it to run the way i intended from the beginning:

    if thread.running = true then

    Initially i got an error message saying i had to create first this object.So i was in the belief the code is wrong, but apparently the property gives that error if the thread instance is not there (yet).

    But, my intention was to test if the thread has terminated itself, which i'm waiting for before i gonna do something else.

    1. how can i test if a thread is still existing ?

    2. how can i differentiate between multiple threads ?

    the thread.running has no reference to a particular thread, so could not address the thread i want.

    The error behavior - btw - is a pain in the ass, because half of my code is code that tries to prevent uncaught .NET errors from happening.Usually one would expect a certain return value if an object does not exists, but in B4P it just throws an ugly .NET exception to the user.To be honest - i don't like that at all.

    Edit: i found a "feature" in my code and just moved the Thread.New1(B4PObject(1)) into the app start section.Now the thread.running does not cause an error anymore.Maybe this solved another issue i was facing with the thread.


    Last edited: Apr 13, 2010
  10. agraham

    agraham Expert Licensed User

    I am afraid that is how .NET (and Java and to an extent C++) is designed to work. They throw execeptions instead of returning error codes. The intention is to better separate error handling code from normal path of execution code.
  11. TWELVE

    TWELVE Active Member Licensed User

    I agree...

    Andrew, can you still answer to the 2 questions above...?


  12. agraham

    agraham Expert Licensed User

    1) If by existing you mean running then ThreadName.Running tells you.

    2) By the name of the Thread object as with any other library object. :confused:
  13. agraham

    agraham Expert Licensed User

    Owing to internal changes in Basic4ppc v6.90 to support typed variables and Sub returns the Threading library needs changing to suit.

    Version 3.0 is posted for Basic4ppc v6.90 and later. I have left version 2.0 available for use with Basic4ppc v6.80 and earlier.

    In version 3.0 the B4PObject(1) parameter is no longer needed in Thread.New.
  14. TWELVE

    TWELVE Active Member Licensed User

    Hi Andrew,

    sorry for the stupid questions.I had paused my main B4P project for one year and a half, meanwhile working and playing with a variety of programming languages, so it is really hard to get into my own code line after that period of time.After some reading and reviewing it is getting better and i recall a lot of things i already knew but apparently shifted into other regions of my brain..*lol*

    Sorry again for asking fundamental questions..certainly i will ask you questions again ( at least for the threading) but before i will continue to read and try to get familiar with my code....;-)


  15. agraham

    agraham Expert Licensed User

    No question is stupid if you don't know the answer (although some can be more nearly stupid than others :)). Just keep on asking.
  16. mjcoon

    mjcoon Well-Known Member Licensed User

    I've just started using the threading library merely because I thought it might provide a better alternative to Shell() (as mentioned in the forum).

    I want to analyse the output of a DOS command (I do not have a need to run on any PPC for this purpose) and hoped to use stdout redirection. But I could not make this work by adding the redirection to the "argument" parameter of Process.Start(); maybe because redirection is a feature of Cmd.exe which is not involved in this process.

    Unfortunately actually using Cmd.exe in the Process.Start(), while it works as intended, briefly pops up the cmd window. Since I am doing this every few seconds in the background the disruption to other windows due to cmd.exe appearing for a fraction of a second in the foreground is intolerable.

    Anyone know of a way round this? Cmd.exe doesn't have a "start minimised" option!

    Alternatively, what I'm trying to do is monitor network access to find what process suddenly downloads 50-80Mb soon after I start my PC but stops if I break the connection briefly. So I am using netstat to monitor traffic and capturing its output. netstat doesn't have an "output to file" option which would obviate the redirection problem. But I cannot find a Basic4PPC library that offers netstat data monitoring functionality. (I thought that network.dll might, but it is focused on client/server operation.)

  17. agraham

    agraham Expert Licensed User

    Have you looked at Wireshark?
  18. mjcoon

    mjcoon Well-Known Member Licensed User

    Thanks Andrew, I hadn't heard of it. (And I asked some ex-work colleagues about the spurious traffic too.) It has help on "Network Troubleshooting" which I hope will lead me in the right direction, though it also says "Wireshark is not aimed (and may not be well suited either) for all the tasks mentioned".

    Separately, I take it that there is no simple answer to running commands and capturing their stdout results...

  19. agraham

    agraham Expert Licensed User

    The method that Process uses to start applications allows the default program to be invoked on say an html or jpg file. This method does not allow redirection.

    There is an alternative way of starting an application that does allow redirection but this will only work directly on exe files. Process could be extended to do this but I have no plans to implement it as I don't think the work involved would be worth the return as I don't imagine this is anything but a niche requirement. I certainly haven't needed it. The only time I have needed to do do this in my Logcat viewer for Android that I have just written but that is a .NET C# application.
    Last edited: Nov 21, 2010
  20. Zenerdiode

    Zenerdiode Active Member Licensed User

    So I'm one step forward and two back in my little program. I need to connect to a variable number of devices and poll them to get a value back; in the order of about 40 at a time. I connect with TCP/IP over a GSM/GPRS carrier - so you appreciate there will be some latancy. I forgot that a Network Client.Connect blocks until the connection either connects or fails. I also want to spin a batton whilst each one is connecting.

    I have built my Client Connecting Sub; dynamically create a number of thread objects, (so each client connection runs in its own thread) wire them to the Sub, but I'm now stuck with how to pass an arguement (the IP addresses of the servers) to each of the threads.

    Is this beyond the scope of the Threading library?
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice