Possible thread issue?

Under a tcpSocket dataAvailable event, I have the following:

[code] receiveBuffer = receiveBuffer + self.readAll

if self.socketHandler.state <> thread.running then
self.socketHandler.run
end if[/code]

The issue I’m having is that at inconsistent and random times, the application is terminating with a ‘ThreadAlreadyRunning’ error at the ‘self.socketHandler.run’ line. How is this possible, if the clause right before it states to run it ONLY if it’s not currently running? This is the only event within that application that calls the run event for the thread, so it’s not another process running before this one is able. Any ideas?

[quote=128202:@Eric Brown]Under a tcpSocket dataAvailable event, I have the following:

[code] receiveBuffer = receiveBuffer + self.readAll

if self.socketHandler.state <> thread.running then
self.socketHandler.run
end if[/code]

The issue I’m having is that at inconsistent and random times, the application is terminating with a ‘ThreadAlreadyRunning’ error at the ‘self.socketHandler.run’ line. How is this possible, if the clause right before it states to run it ONLY if it’s not currently running? This is the only event within that application that calls the run event for the thread, so it’s not another process running before this one is able. Any ideas?[/quote]

Why not use a flag instead ? You set it true when you run the thread, and set it false at the end of the thread run event. So if the flag is true, you do not run the thread again.

I think you should test for thread.notrunning rather than <> thread.running. A thread may be running, but suspended, sleeping or blocked.

My suspicion is that when the dataavailable event fires a second time the thread is suspended due to the main loop being active.

That’s likely what I’ll do, as it seems it’s the only alternative to the solution. Just curious as to why the original method is failing, when it appears it shouldn’t be. Don’t understand how a thread that is running gets through the logic, called right before the run is initiated again. Appreciate the reply Michel.

[quote=128206:@Wayne Golding]I think you should test for thread.notrunning rather than <> thread.running. A thread may be running, but suspended, sleeping or blocked.

My suspicion is that when the dataavailable event fires a second time the thread is suspended due to the main loop being active.[/quote]

That’s possible… didn’t think of that. And, as crazy as this seems… I didn’t even realize there was a ‘.notRunning’ property for a thread. It wasn’t in the documentation for threads, and I obviously don’t always pay attention to the autosuggestions after the ‘.’ Thanks Wayne.

It’s not a property, it’s a possible value of socketHandler.State. There are 5 possible values for State

Running
Waiting
Suspended
Sleeping
NotRunning

Only the last one, NotRunning, is safe to call Run.

Ah, I see it… don’t know how I overlooked that. o.O