views:

298

answers:

1

I am developing a project for BB. The application works with the network and sends / receives data via HTTP. Now I use the queue and queue manager. Manager starts with a background thread and works in while (true) loop, checking the queue for new transactions to the server. If the queue is not empty, then the transaction is executed, otherwise the manager goes to sleep for 200 ms.

The process of the transaction as follows: - Runs another thread (using the Runnable), which opens a connection to the network and first thread waiting for background thread or timeout (and for that we need a loop), which we set. - If the connection is established, then starts another thread (using the Runnable), which runs getResponseCode (), and first thread waiting for background thread or timeout (and for that we need a loop), which we set.

Before it, we showing popup window with wait-rotating-image, and after it is removed. It synchronized via Application.getEventLock ().

Iit unstable sometimes and thread sleeps for a long time ignore timeout-waiting-loop.

I would like to know how valid such an approach, what advice and best-practice is, what is your experience?

I use 4.5, 4.6, 4.7 and 5.0.

A: 

The lock returned by Application.getEventLock() should only be used for code that modifies the UI or UI components - it's the lock used by the event dispatcher. You should not be using it for background tasks such as HTTP processing. If you want to synchronize that code, it would be best to just create your own lock object.

Marc Novakowski
Can I use invokeLater and invokeAndWait for modify UI?
Yeti
Yes, that's usually how you "wrap" UI calls when you're in a non-UI thread.
Marc Novakowski