Now I could be making a mistake and doing this completely wrong, but sometimes the Wait For doesnt work.
Here is the code I am using:
	
	
	
	
	
	
	
		
			
			
			
			
			
		
	
	
	
		
	
	
		
	
So above, I am setting up asyncstreams and waiting on a response or a disconnection from the server. Most of the time it works fine. if astream error, terminated, or newdata is fired, I call the Auth_Complete sub which is just a wait-for catch. I dont know if I am doing this right though.
sometimes itll see this catch and resume from the wait for, sometimes not.
any ideas? Thanks.
			
			Here is the code I am using:
			
				B4X:
			
		
		
		'This routine will verify the authorization for Server Access.
Sub CheckAuthorization As ResumableSub
    Dim AuthSocket As Socket
    AuthSocket.Initialize("AuthSocket")
    AuthSocket.Connect(Address, Port, Timeout)    'Connect to Server
    Wait For AuthSocket_Connected (Successful As Boolean)
    If Successful = False Then
        ToastMessageShow("Unable to connect to the server at this time. Please check your connections and try again.", True)
        ProgressDialogHide  
        Return False    'This routine failed. Lets return a boolean.
    End If
    If AuthSocket.Connected = True Then 'Lets make sure we are still connected to the server.
        AstreamsAuth.Initialize(AuthSocket.InputStream, AuthSocket.OutputStream, "AstreamsAuth")
        AstreamsAuth.Write(("GT " & Account.Username & " " & Account.Password).GetBytes("UTF8"))
        Wait For Auth_complete (Result As Boolean)            'Wait for the data to come back from the socket. (or error)
      
       'Sometimes we dont make it here. The above Wait For event doesnt respond.
        If Result = True Then
            Log("Account Authorized")
            ProgressDialogHide
            Try
                AstreamsAuth.Close
                AuthSocket.Close
            Catch
                Log("Socket closed already")
            End Try
            Return True
        Else
            Log("Account Not Authorized. Reason: " & Account.AuthStatus)
            ProgressDialogHide
            Try
                AstreamsAuth.Close
                AuthSocket.Close
            Catch
                Log("Socket closed already")
            End Try          
            Return False
        End If
    Else
        ToastMessageShow("Unable to connect to the server at this time. Please check your connections and try again.", True)
        ProgressDialogHide  
        Try
            AstreamsAuth.Close
            AuthSocket.Close
        Catch
            Log("Socket closed already")
        End Try
        Return False    'This routine failed. Lets return a boolean.       
    End If
End Sub
Sub AstreamsAuth_NewData (Buffer() As Byte)
    Try
        Dim ResponseString As String = Common.RemoveSpecialCharacters(BytesToString(Buffer, 0, Buffer.Length, "UTF8"))
        #if DEBUG
            Log(ResponseString)
        #End If
        Dim ResponseArray() As String = Regex.Split("\|", ResponseString)
      
        Account.AuthStatus = ResponseArray(0)
      
        'Lets verify the authorization
        If ResponseArray(1) = "1" Then     'We are authorized.
            Account.AuthorizedBots.Initialize
            Account.AuthorizedBots.AddAll(Regex.Split(",", ResponseArray(12)))    'Add all of our bot IDs in the list.
            Account.Expiration = Common.Left(ResponseArray(17), 10)
            Log("Ret: Authorized")  'This works, puts a log in the IDE
            CallSub2(Me, "Auth_Complete", True)  'This doesnt work sometimes. its hit or miss.
        Else    'We are not authorized.
            Account.AuthorizedBots.Initialize
            Account.AuthorizedBots.Clear
            Account.BotID = ""  
            Log("Ret: Not Authorized")
            CallSub2(Me, "Auth_Complete", False)
        End If
    Catch
        Account.AuthStatus = "Incorrect Response from Server"
        CallSub2(Me, "Auth_Complete", False)
    End Try
End Sub
Sub AStreamsAuth_Error
    Try
        ToastMessageShow("Dropped connection to Server. Reason: " & LastException.Message, True)
    Catch
        Log("Error: " & LastException.Message)
    End Try  
    Account.AuthStatus = "Unable to complete login request."
    CallSub2(Me, "Auth_Complete", False)
End Sub
Sub AstreamsAuth_Terminated
    'ToastMessageShow("Dropped connection to Server", True)
'    Account.AuthStatus = "An error has prematurely ended the connection to the server."
'    CallSub2(Me, "Auth_Complete", False)  
End SubSo above, I am setting up asyncstreams and waiting on a response or a disconnection from the server. Most of the time it works fine. if astream error, terminated, or newdata is fired, I call the Auth_Complete sub which is just a wait-for catch. I dont know if I am doing this right though.
sometimes itll see this catch and resume from the wait for, sometimes not.
any ideas? Thanks.
 
				 
 
		 
 
		 
 
		 
 
		 
 
		 
 
		 
 
		 
 
		 
 
		 
 
		 
 
		