B4J Question Run processes simultaneously

Discussion in 'B4J Questions' started by jroriz, May 28, 2019.

  1. jroriz

    jroriz Active Member Licensed User

    Hi.
    I would like the Sub LongProcess to be executed SIMULTANEOUSLY, ie the 3 processes beginning and ending at the same time.
    As it is in the code, they start together, but the second only ends after the end of the first, and the third after the end of the second.
    Can anyone help?

    Code:
    Sub AppStart (Form1 As Form, Args() As String)
        MainForm = Form1
        MainForm.Show
        
        LongProcess(
    0)
        LongProcess(
    1)
        LongProcess(
    2)

    End Sub

    Sub LongProcess(ix As Int)
        
    Log("start time: " & ix & " " & DateTime.Time(DateTime.Now))
        Sleep(
    0)
        
    For x = 1 To 300000000
            
        
    Next
        
    Log("end time: " & ix & " " & DateTime.Time(DateTime.Now))
    End Sub
    log:
     
  2. DonManfred

    DonManfred Expert Licensed User

    Watch the resumeable subs tutorial and get familar with resumeable subs.
     
  3. jroriz

    jroriz Active Member Licensed User

    Thanks for your attention. This was the best I got after following the tutorial. I made several attempts but I did not succeed.
     
  4. agraham

    agraham Expert Licensed User

    In a B4J program all your code runs on the single main thread so you cannot achieve true simultaneous execution. Even using Resumable Subs you will still get consecutive execution of your Subs. You can achieve a measure of simultaneity by running each Sub on a separate thread, each of which might execute a different core of your CPU or might be time sliced with other threads on the same CPU core. What actually happens is outside your control.

    Even if you use threads although the processes may start at the same time they will never all end at same time owing to uncertainties in the execution process.

    You don't say why you want to simultaneous execution but I doubt that it is really necessary. If you want to try my B4A Threading library https://www.b4x.com/android/forum/threads/threading-library.6775/ should work in B4J in release mode.
     
    MarkusR and jroriz like this.
  5. jroriz

    jroriz Active Member Licensed User

    I need to show 4 different information on the screen, starting from a common information.
    The problem is that each of these information takes about 4 seconds to complete, and one does not depend on the other.
    My wish is to finish the process in 4 seconds, doing the four processes together.
    I do not know if I could make myself clear.
    I will try your lib.
    Thanks.
     
  6. agraham

    agraham Expert Licensed User

    You cannot directly access screen elements from a thread. You will need to call a Sub that runs on the main thread to access screen elements using the RunOnGuiThread method.
     
  7. MarkusR

    MarkusR Well-Known Member Licensed User

    nonstop or is there a delay as example wainting for a http response?

    its more like this, as soon you use sleep the sub interrupts and other continue until the sleep there.
    Code:
    Sub LongProcess(ix As Int)
        
    Log("start time: " & ix & " " & DateTime.Time(DateTime.Now))   
        
    For x = 1 To 100000
            Sleep(
    0)
        
    Next
        
    Log("end time: " & ix & " " & DateTime.Time(DateTime.Now))
    End Sub
     
    Last edited: May 28, 2019
  8. jroriz

    jroriz Active Member Licensed User

    In fact it is a mathematical process that has 5000 interactions.
     
  9. MarkusR

    MarkusR Well-Known Member Licensed User

    then i think the resumeable subs make no sense for you.
     
  10. Erel

    Erel Administrator Staff Member Licensed User

    You should do it like this:
    Code:
    Sub LongProcess(ix As Int)
        
    Log("start time: " & ix & " " & DateTime.Time(DateTime.Now))

        
    For x = 1 To 300000000
            
    if x Mod 1000000 = 0 Then Sleep(0'try different values
        Next
        
    Log("end time: " & ix & " " & DateTime.Time(DateTime.Now))
    End Sub
    This will allow you four processes to run together (on the same thread, but still together).
     
    MarkusR likes this.
Loading...
  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