Sleeping the main thread

  1. 3 years ago

    Tim S

    23 Jul 2016 Canterbury, UK

    Until case 42544 is fixed, I'm looking at coding up a workaround. This case relates to the fact that, if two threads try to write to an SQLite db at the same time, one may get "database is locked". Ordinarily, setting an SQLite timeout should cause one thread to sleep for a while and try again later. This is handled inside the SQLite library, but it just uses an ordinary system sleep, which hangs the whole app until the timeout expires.

    I thought I would just leave the timeout at zero, which avoids SQLite itself trying to handle the error. I can do thread.sleep() myself which allows the other thread to proceed. This should be easy enough, but suppose the thread needing to sleep is the main thread. How do I sleep the main thread?

    app.SleepCurrentThread

    But putting that code in a thread or timer might be better.

  2. Tim H

    23 Jul 2016 Pre-Release Testers Answer Portland, OR USA

    app.SleepCurrentThread

    But putting that code in a thread or timer might be better.

  3. Michel B

    23 Jul 2016 Pre-Release Testers RubberViews.com

    You may want to pause the other thread instead, which would avoid having you app freeze on the user.

  4. Kem T

    23 Jul 2016 Pre-Release Testers, Xojo Pro, XDC Speakers New York

    Or use a semaphore?

  5. Tim S

    23 Jul 2016 Canterbury, UK

    I'll try the simple approach first - doing exponential backoff. So thanks Tim for the hint about sleeping any thread.

    I take the point about the app freezing but the trouble is most actions in my UI involve some database activity. I'll see how it goes.

or Sign Up to reply!