We would like to build a community for Small Basic programmers of any age who like to code. Everyone from total beginner to guru is welcome. Click here to register and share your programming journey!


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Threads
#11
(translated by Google translator)

Hello, JR  Smile

If I understood you correctly, then now everything works as you described.
  1. A small event handler sets the appropriate flag.
  2. The main thread checks the value of this flag at the time the developer needs it.
  3. If the flag is set, the MAIN thread calls the required routine, which will be executed in this MAIN thread.
In the case when an immediate program response to an event is required, then the event handler ITSELF executes the necessary code in its parallel thread.
If the event handler calls any subroutine, then this subroutine will also be executed in the handler thread ( that is, in a parallel thread ).

I hope I haven't made a mistake anywhere.  Blush
[-] The following 2 users Like AbsoluteBeginner's post:
  • jrmrhrb00, z-s
Reply
#12
Z-S,

If putting in a current thread property in your extension would work that would be great! Then we would really know.

JR

AB.

Your comments are right if in fact a subroutine does not call a different thread and stays in the UI thread when called.

JR
Reply
#13
(11-26-2024, 12:56 PM)jrmrhrb00 Wrote: AB.

Your comments are right if in fact a subroutine does not call a different thread and stays in the UI thread when called.

JR
(translated by Google translator)

JR,
I suggest discussing everything as precisely as possible. This might be useful for someone else learning Small Basic.  Shy

There are two different objects in my consciousness:
  1. subroutine
  2. and event handler
If I'm not mistaken, the subroutine is executed in the thread that called the subroutine.
But, the event handler runs in its own parallel thread.
A running subroutine calls another subroutine on the same thread on which it itself is running.
To call another subroutine in a new thread, you must use the LDCall.CallAsync(dll, extension, obj, method, arguments) method.

Do you agree?
[-] The following 1 user Likes AbsoluteBeginner's post:
  • jrmrhrb00
Reply
#14
AB,

Question 1: If a subroutine is in fact called by the UI and actually runs in the UI. I am not really sure of that.
Question 2: I have read that the event handler runs only in the UI thread.
Question 3: Maybe a running subroutine will call another subroutine on the same thread.
Question 4: I don't know anything about LDCall.

The best guidance I have read on threading has came from LitDev. It is a very complex topic and really shouldn't be a part of the discussion for Small Basic.
Reply
#15
Hi,

All subroutines run on the thread that they were called from, so a call to a subroutine and all child subroutines will be on the same thread as their top level parent.

The main program starts on the UI thread, so all subroutines called from it will also be on the UI thread.

With the exception of specific extension thread control, All EVENT subroutine start on their own (non UI thread), and therefore subroutine calls made from with an EVENT subroutine will also be on the event thread.

1] Yes, the main program is UI thread and all subroutines called from it also are.
2] Exactly the opposite, all event subroutines are NOT on the UI thread, they have their own
3] Yes, as above
4] LDCall can run things async (on a different thread)

One thing to note is that updates to the UI (e.g. moving a shape or anything else) will only take place on the UI thread.  If such a UI update is made in an event subroutine, then it wil not actually be done until the event subroutine ends.  Also, any variable changes made in event subroutines may conflict with asynchronos UI code (running at the same time, for example the same loop index 'i' may be over-writen by event sub while it is being used in UI thread).  These are why I always suggest to do as little work in event sub as possible, just set a flag, and handle it in an 'game loop' on the main UI thread.
[-] The following 3 users Like litdev's post:
  • AbsoluteBeginner, jrmrhrb00, z-s
Reply
#16
Thanks to Z-S and his extension here is a program that shows what thread is actually being used and for what function: XKMS572.000

The program is basically the first program in LitDev's Threading article. Modified a little bit. 

JR
[-] The following 2 users Like jrmrhrb00's post:
  • litdev, z-s
Reply
#17
If you have latest zs extension version you could play with this.

ZSThreading : for multiple threads in sb

ZSTask : for asynchronous non blocking function different from thread https://en.m.wikipedia.org/wiki/Asynchro...ogramming)
ZS
[-] The following 1 user Likes z-s's post:
  • jrmrhrb00
Reply
#18
Z-S,

I have your new extension and tried using the thread functions and I couldn't get them to work. Could you give us some examples of how to use them?

Thanks,

JR
[-] The following 1 user Likes jrmrhrb00's post:
  • z-s
Reply
#19
ZSTHREAD SAMPLE
Code:
TextWindow.WriteLine(ZSUtilities.GetCurrentManagedThreadId())
Sub loop
        For i = 1 To 50
              TextWindow.WriteLine(i)
               TextWindow.WriteLine(ZSUtilities.GetCurrentManagedThreadId())
               Program.Delay(5000)
        EndFor
EndSub
ZSThread.RunSub("loop")

ZSTASK SAMPLE
Code:
TextWindow.WriteLine(ZSUtilities.GetCurrentManagedThreadId())
Sub loop
        For i = 1 To 50
              TextWindow.WriteLine(i)
               TextWindow.WriteLine(ZSUtilities.GetCurrentManagedThreadId())
               Program.Delay(5000)
        EndFor
EndSub
ZSTask.RunSub("loop")
ZS
[-] The following 1 user Likes z-s's post:
  • jrmrhrb00
Reply
#20
Z-S,

Thanks for the samples! They did get me started.

JR
[-] The following 1 user Likes jrmrhrb00's post:
  • z-s
Reply


Forum Jump:


Users browsing this thread: 4 Guest(s)