B4J Question How to raise custom events

Locutus

Member
Licensed User
Longtime User
Hi all, I am working on a server program to run on Windows and Linux using B4J. When the program receives a new connection I want to be able to raise an event so the event handler runs asynchronously from the new connection function. Something like this pseudo code.

B4X:
Sub Server_NewConnection (Successful As Boolean, NewSocket As Socket)
    Log("New Connection: ")
    If Successful Then
        Dim astream As AsyncStreams
        astream.InitializePrefix(NewSocket.InputStream, False, NewSocket.OutputStream, "astream")
        Select "controlcode from stream"
        Case "code1"
            Raise event For callCode1Function
        Case "code2"
            Raise event For callCode2Function
        End Select
    Else
        Log(LastException)
    End If
    server.Listen
End Sub

The idea is to receive a connection. Raise an event for a function according to the type of connection then go back to listening for a new connection while the function runs.

I hope I was able to explain myself clearly enough.

Is there a method of creating a custom event and calling it? If so how and are there any examples I can view? I tried searching the forums and all I could find was CallSub but I don't know if it returns immediately and I couldn't get it to call a local function.

Even better if possible is to put each new connection into a separate background thread. Is that possible?
 

agraham

Expert
Licensed User
Longtime User
Each AsyncStreams input stream (and output stream) runs in its own separate thread but raises the NewData event on the main GUI thread so that event code can safely access GUI elements, or call other Subs that do so, if required. I would have thought this would suffice for what you want.
 
Upvote 0

Locutus

Member
Licensed User
Longtime User
Thanks for your reply agraham. My concern is for multiple connections to happen in a short time. I am under the impression if the NewData event is not cleared asap then there is a bottleneck occurring. I could be wrong.

So I wish for the NewData event (better suited it is true :) ) to raise another event and leave itself free to be called again. This is a console app so there is no GUI to contend with. It is a server application so if there are too many active things happening on the main thread it will slow or worse, freeze. If I could shunt each new connection or work associated with that connection to different threads there will be no slowdown on accepting new connections. I do a similar thing with Qt's signal and slots methodology or Androids/Java's listener.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
NewData events, like Timer events, are queued on the main thread message loop and handled in turn. Assuming that you have a multi-core CPU or multiple CPUs and that the OS thread scheduling takes advantage of this then I can see that you could handle more events by shunting them off to their own thread, although the amount of work each of those threads needs to do would need to substantial as the overhead of creating the thread will be incurred on the main thread anyway.

I have tested my Threading library on B4J and as expected it works as it is pure Java with no B4A dependencies. If you don't already have it you can find it here.
http://www.b4x.com/android/forum/threads/threading-library.6775/

I've attached a B4J demo equivalent to the B4A one so you can see that it works.

EDIT :- Mind you I would just try it to see if there is a real problem or a theoretical one. Adding the threading later should be pretty easy.
 

Attachments

  • B4JThreadingDemo.zip
    1.7 KB · Views: 370
Upvote 0

Locutus

Member
Licensed User
Longtime User
NewData events, like Timer events, are queued on the main thread message loop and handled in turn. Assuming that you have a multi-core CPU or multiple CPUs and that the OS thread scheduling takes advantage of this then I can see that you could handle more events by shunting them off to their own thread, although the amount of work each of those threads needs to do would need to substantial as the overhead of creating the thread will be incurred on the main thread anyway.

I have tested my Threading library on B4J and as expected it works as it is pure Java with no B4A dependencies. If you don't already have it you can find it here.
http://www.b4x.com/android/forum/threads/threading-library.6775/

I've attached a B4J demo equivalent to the B4A one so you can see that it works.

I did see that library and was going to give it a try but wasn't sure if it would work on B4J. Now that you have confirmed that it does I will definitely be giving it a go. I am not sure how much work each thread would need to do but it would mostly be involving database queries to a separate server so there could be an amount of waiting involved due to network lag. So I want the processes to be as asynchronous as possible. Putting them on a different thread for each connection will prevent blocking of the main thread from accepting other connections.

Thank you for your input.
 
Upvote 0

Locutus

Member
Licensed User
Longtime User
Thanks Erel, I am actually using your excellent B4J CCTV example as a template for my server. (you probably recognised the code from the snippet I posted earlier).
 
Upvote 0
Top