Hi.
The purpose of the following code is to test how a Wait For (trigger) will pause the loop until some condition is met.
Except that it doesn't work; the loop just ignores the Wait For.
So I guess the question is how do I implement a semaphore in B4x.
The purpose of the following code is to test how a Wait For (trigger) will pause the loop until some condition is met.
Except that it doesn't work; the loop just ignores the Wait For.
So I guess the question is how do I implement a semaphore in B4x.
Wait For Test:
#Region Project Attributes
#MainFormWidth: 600
#MainFormHeight: 600
#End Region
Sub Process_Globals
Private fx As JFX
Private MainForm As Form
Private xui As XUI
Private Button1 As B4XView
Private bc As ByteConverter
Private b(20) As Byte
Private strToReceive As String
Private noStringsReceived As Int
Private bigString As String
Private RxDataComplete As Boolean
End Sub
Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.RootPane.LoadLayout("Layout1")
MainForm.Show
End Sub
Sub Button1_Click
bigString = ""
RxDataComplete = True
strToReceive = "My new String"
trigger
For k = 0 To 4
Log(DateTime.Time(DateTime.Now))
wait for (trigger) Complete(ret As Boolean)
Log(DateTime.Time(DateTime.Now))
b = bc.StringToBytes((strToReceive & " " & k), "UTF8")
strm_NewData(b)
Next
Log("All done")
End Sub
Private Sub strm_NewData(buffer() As Byte)
strToReceive = bc.StringFromBytes(buffer, "UTF8")
Log(strToReceive)
ProcessData(strToReceive)
End Sub
Private Sub ProcessData(s As String)
Sleep(2000)
noStringsReceived = noStringsReceived + 1
bigString = bigString & s & Chr(10)
Log(bigString)
If noStringsReceived < 5 Then
Log("No Received " & noStringsReceived & " " & DateTime.Time(DateTime.Now))
End If
Sleep(10)
End Sub
Private Sub trigger As ResumableSub
Sleep(1)
Return True
End Sub