Android Question how to run sequence of subs

Discussion in 'Android Questions' started by Mousa Najafi, Feb 13, 2015.

  1. Mousa Najafi

    Mousa Najafi Member Licensed User

    I have a desktop server which is written in VB.net I want to send commands to it through B4A code
    commands should be send to server sequentially
    after each command, socket and stream should be closed and connected again to be ready to next command. I can send this sequence of commands by separate button click events
    but I need this sequence to be done automatically not by button clicks.
    I tried to use timer and threading library but not succeeded as I am not professional on threading
    would you help me to solve this
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    Don't use the Threading library. It will only make it more complicate.

    Timer should work exactly like button clicks.
     
  3. Troberg

    Troberg Well-Known Member Licensed User

    Isn't it better to trigger the sending of the next command when the previous operation is complete?

    Or, if there are no ongoing command, simply send it as it appears (I assume you get the need to send a command from some kind of event).

    Seems simpler and more effective than a timer.
     
  4. Mousa Najafi

    Mousa Najafi Member Licensed User

    I've used asyncstream I don't know how to create a sequence of subs that run after each other( sub 1 completes then sub 2 runs and so on) here is the code sample I used to send command
    Code:
    Sub btnConnect_Click
        Socket1.Initialize(
    "Socket1")
        Socket1.Connect(
    "192.168.2.4",1925,5000)
        ServerSocket1.Initialize(
    1927,"server1")
        ServerSocket1.Listen
    End Sub
    Sub Socket1_Connected(Connected AsBoolean)AsBoolean
              
    If Connected = True Then
                   
    ToastMessageShow("Connected",True)
                   Astreams.Initialize(Socket1.InputStream,Socket1.OutputStream,
    "Astreams")
                   clientconnectedsuccessful = 
    True
             
    Else
                    clientconnectedsuccessful = 
    False
             EndIf
             btnConnect.Enabled = 
    True
            
    Return    clientconnectedsuccessful
    End Sub
    Sub SendstrData(str AsString)
            Astreams.Write(str.GetBytes(
    "UTF8"))
            Astreams.Close
            Socket1.Close
            btnConnect_Click
    End Sub
    Sub sendbytedata(buffer() AsByte)
            Astreams.Write(buffer)
            Astreams.Close
            Socket1.Close
    End Sub
    Sub btnTest_Click
             SendstrData(
    "[cmd_broadcast]"& MyIP)
    End Sub
    Sub btnconnect2_Click
             sendbytedata(ArrayAsByte(
    128,10))
    End Sub
    please tell me how to use timer in the above code to act like the btnTest_click then btnconnect2_click events
    consider socket_connected event also should be completed after clicking btntest_click because it connects the socket in the sub sendstrdata(str as string)

    I highly appreciate your help I get stuck to solve this problem
     
  5. DanteS

    DanteS Member Licensed User

    To control a process step by step, you usually need these elements:
    A timer, a select case structure and a step variable.
    The secuence is something like this:
    You start the process clicking a button.
    Inside the button click event, you set the step variable to 1, for example, and enable the timer. This click also runs the first step of the process.
    In the case 1 section of the select case structure (inside the tick event of the timer) you check if the first step of the process is over.
    If affirmative, you set the step variable to 2, and start step 2, and so on.

    The timer interval should be set to the proper value, depending of the reaction time of the controlled process, in order to check for a response with a reasonable frequency


    <code>

    Sub timer1_tick

    timer1.enabled=False
    select case stepX
    case 1
    if login_ok then
    stepX=2
    timer1.enabled=True
    ProcessCtrl("show alarms")
    end if
    case 2
    if alm>"" then
    sendemail(alm05)
    end if

    stepX=3
    timer1.enabled=True
    ProcessCtrl("new data")
    case 3
    if load_ok then
    ProcessCtrl("report")
    stepX=9
    else
    ProcessCtrl("alm77")
    stepX=4
    end if
    case 4
    ....
    End Select

    End Sub
    </code>
     
  6. Troberg

    Troberg Well-Known Member Licensed User

    But, when it comes to communication, the state code should not be in a timer event, it should be in the data received event. Otherwise, you risk sloppy performance, buffer overruns, excessive CPU use or difficult to find bugs.
     
  7. Mousa Najafi

    Mousa Najafi Member Licensed User

    Thanks I will try both suggestions to overcome this problem and will post result .
    I couldn't been able to get the reliable result with timers as tick event conflicts with the asyncstreams
    (It is unclear to set the proper timer interval as sending and receiving time from client to server differs time to time)
     
  8. Mousa Najafi

    Mousa Najafi Member Licensed User

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