running a thread from a timer

I have a function being called from a timer that is blocking the user interface, so I tried creating a thread and running the thread from the timer. This works, but the timer action is still blocking while the thread action runs. What’s the proper way to do this?

Please, can you post a sample of your code?

Suggest you also explain what you are trying to do.

Sure. What I’m trying to do is detect when devices are connected and disconnected to the serial ports, and establish connections with ones that respond to a particular query. I’ve got a timer object with the following code in its Action event ( One port is checked each time the timer event is called.):

if PortOpen then
Serial1.Close
PortOpen = False
end if

CurrentSerial = CurrentSerial + 1
if CurrentSerial >= System.SerialPortCount then
CurrentSerial = 0
end if

if System.SerialPortCount>0 then
Serial1.SerialPort = System.SerialPort(CurrentSerial)
end if

if PortOpen = False and ConnectionThread.State = Thread.NotRunning Then
ConnectionThread.Run
end if

In the ConnectionThread Run Event Handler I’ve got this:

if Serial1.Open then
PortOpen = True
SendQuery(“XXX”)
end if

Initially I was sending the query directly from the timer, but this was causing the app to freeze for a couple hundred ms at a time. After this change the freezes are even longer. Aside from the freezes the code works.

I think, like FolderItem actions, SerialPort actions are UI locking in Xojo regardless of whether they are threaded or not. If I am mistaken, I would love to find out! I have seen this delay and come to expect it any time I gather the available serial ports.

Ah - that’s annoying! I guess it’s time to go with a slightly less lazy method and only query the ports once, and repeat if I detect a new port!

In the Xojo Examples is a program from Paul that shows how to do this (if i am not mistaken):
New Project:
Communication/Serial/Serial Port Bar Code Reader Example

In that program the availability of serial ports is constantly checked and the list with active ports is adjusted.

Maybe that example can help you.

I just updated as described above and that fixes the problem since I only have to check each serial port once, then again if a new device is connected.

Andre - I have looked at the Bar Code Reader app and it was a useful template for detecting serial ports, but it isn’t opening them except when the user asks it to, so the delay in opening the port isn’t a factor.