Application.SleepCurrentThread

The doc for SleepCurrentThread says that it may be used for any thread. Can I take it, therefore, that this includes the main thread? And, if the main thread is sleeping, does this mean that other threads can run? Are timers and events blocked if the main thread is sleeping?

No - the main run loop isn’t really a “thread” in the Xojo sense of the word.

And yes to the second part. Not a good idea to sleep the main run loop

OK. What happens, then, with SQLite when a busy-timeout is set to avoid getting “Database is locked”? That seems to call the OS sleep mechanism, judging by the SQLite source. If I want to have a number of threads (including the main thread) accessing some number of databases, then I need either to use the timeout feature supplied, or implement my own. Either way, for the main thread that seems to imply a problem.

Should I avoid having the timeout for SQLite calls on the main thread?

If you want to see how mad the OS gets at you go ahead and try “App.SleepCurrentThread(30000)”. :slight_smile:

I can’t see any reason you would like to do that unless you want to test how angry people get with the damn beachball or the spinning wheel of death.

1 Like

Perhaps Tim’s idea was that if you could sleep the main “thread”, other threads would speed up


If a database operation is done on the main thead, and you want to avoid getting “Database is locked”, you have to sleep the main thread and try again in the case where the operation returns SQLITE_BUSY.

You may be using the wrong hammer. Look at @olivierV issue and @Christian_Schmitz solution on this thread: The entire app hangs during the SQLITE timeout. This may be very similar to what you are attempting to work around.

Oh sure. I’ve been doing some testing today, and the entire app will hang during the period that an SQLite call on the main thread has to wait for another program or thread to give up a lock it has on a database. And there’s no way to avoid this, assuming you don’t want the SQLite call to immediately return SQLITE_BUSY (Database is locked).

If you set a timeout of (say) 60 secs on a connection, and the SQLite select you are doing in your Xojo app cannot proceed because another task (such as the sqlite3 CLI in a Terminal window) has an exclusive lock on the database, then after 60 secs your app will get SQLITE_BUSY from that select attempt - unless the other program relinquishes the lock. As soon as it does that then your app’s select succeeds and everything is peachy.

Xojo’s implementation for this appears to internally retry the operation in a tight loop, using 100% CPU. My testing showed that the Xojo app ‘sees’ the lock being released pretty much immediately.

Sorry, what I meant to say was the perhaps you could try Christian’s implementation (i.e. “different hammer”) and it could give you better results. I do realize that comes with a cost (both monetary and time-wise) and is probably not what you were asking for. Again, my bad for not really reading more carefully. I do still maintain that you don’t want to sleep the main thread though; either OS just punishes the app badly when you do this (and the user may start throwing things at the screen - I caught myself doing that).