Thread question

Hi there,

I had a method, called by a timer action, that took up to 5 secs to process.
So I placed the long running code in a separate thread. There’s a new thread instance created every time.

I created myThread subclassing a Thread, passing a parameter in the constructor and holding a few properties.

The code is really simple:

Dim myT As myThread
myT = new myThread(param)
myT.Run

The long processing is placed in the Run event of the thread.

I’m testing and see that it takes up to 5 secs just to perform these few lines (create the new thread instance and perform thread.run).
Like the thread is not running in background. No difference with a direct method call.

I guess I’m missing something obvious. What’s wrong ?

Any idea ?

Using Xojo 2015r1
Desktop App (OSX)

a thread doesn’t make it faster.
It only allows you to keep the main thread responsive to user input.
And only that if you use thread friendly methods.

Hi Christian,

I know this perfectly.
Maybe my explanation isn’t clear.
The problem is that creating the thread and just performing thread.run takes 5 seconds.
I’m still expecting the 5 seconds, but this 5 secs processing should happens inside the new thread.
Here it seems the app hangs as long as the thread run event has not finished.

I’m not sure what you call “thread friendly methods” but inside the thread’s run event I’m just connecting to a ENetServer (Einhugur plugin).

Some activities - especially from plugins - may block the UI. Ask Björn if connecting to the ENet server is thread-friendly or not.

it could be a synchronous connect, so whole app has to wait.

Thanks Beatrix & Christian.
It seems you’re right, I’ll ask Bjorn anyway to be sure.

Also, make sure you haven’t overloaded the Run method. Ie., you don’t have a method in your subclass named Run.

Hi Tim,

Yes, I’m sure.
I just have a Constructor method and a few properties.

Then it must be the plugin.

Or some built-in method that doesn’t yield time by default.

The run event contains the connection to ENetServer.
The app is hanging as soon as the thread.run method is called.
If I add

me.sleep(10000)

before trying to connect to the ENetServer my app is not hanging before the 10sec are elapsed.

I guess it confirms that the plugin is responsible.

I’ll check with Bjorn.

ENet by nature will not hang your application since you control its flow and times with the ENet’s Poll commands, thats part of the whole concept of ENet. You should not even need a thread to run it, usually one controls the poll command just with a timer and the applications will not hang.

If you do put in in thread then the thread needs to own the whole thing and control the poll command.

Your post here though does not include much to go on what or how your thread is actually doing things.

Ok so been reading it over and over again to try to make some sort of answer that makes sense.

For individual commands then a Xojo thread is not going to stop anything from hanging since a Xojo thread is not a real thread, so anything under the hood in the OS or protocol layer is going to hang it for sure. Only a real thread would prevent such.

For ENet then healthy control of the Poll on sever and client side is usually your friend to make things run smooth.

If there is actual good actual case where hanging cannot be prevented then we could look into making Async connect method (assuming its the Connect method your having trouble with ?) that under the hood would use actual OS thread.

ENET was designed with thread safeness in mind (99%).

That would be awesome, even more running on a separate thread and avoid Polling it making it really async. But I don’t know if that’s plausible in Xojo now. Polling ENET within Xojo makes it slow and would be perfect to run it on a different thead and communicate to the main Xojo thread via Events. (No Polling)

ENET is really one of the best Xojo plugins! :slight_smile:

Hi everybody,

I think I need to give more details about my apps.

On one side I have console apps (multiple instances running on different servers) running background tasks, defined as ENet servers and broadcasting informations once peers are connected. These apps are running fine.
These Enet servers are not always active, thus not always listening for clients.

On the other side I’d like to have client desktop apps, ENet Clients, connecting to one or more ENet servers.
The users can select which servers they want to connect to.
When the servers are active everything is smooth. When a server is not active, the server.connect takes a few seconds before returning a false value. At this point the app freeze, hence my idea to put the server.connect for each server in a different thread.
But the app freeze on server.connect.

My problem is not that server.connect takes a few seconds but rather that the app is frozen while doing so.

Running the ENetClientTest sample app from Einhugur (does not use threads) when the ENetServer is not listening illustrate this.

Anyway, thanks for your answers.

Yes I would think that we would need to make some sort of Async connect method for you that internally uses real OS thread.

Or if someone has a plugin that exposes a real thread then that works too. (at least to test it)

I’m not familiar with ENET, but could you use a tcpsocket to test the availability of the service before you attempt a connection?

Just bit off-topic question: Would if be possible to create ENET plugin running on a different thread and with the already bilt-in packet class, made a PacketReceive event where no polling in need within Xojo? Pure Async in Connect/Received, etc.

Unfortunately it’s not allowed to use a TCP connection for ENET (UDP prop protocol). Will simply not fire the event up.

I’m gona see about just creating general OS Thread class for those that are brave and know what their doing then people can do what they want with any part of the ENET. Thats probably the first easy step.

Then to maybe add ConnectAsync which has just the connect method threaded for those that needed it easy and just the Connect to be threaded.