0
ImageThread it = new ImageThread(this.imageURL,this);

        Thread t = new Thread(it);
        t.start();

I'm new to threads and had to implement the above in my field that loads an image, because it slowed down the UI thread.

Whether my threads are downloading an image or some json content, they appear to still be downloading even though the user has pushed a new mainscreen onto the uiapplication. This continuous loading could be a problem if a user enters a screen and then accesses another in quick succession. As a result, the last screen they are on only finishes its thread when the others are done.

What am I supposed to do with my threads that would be deemed responsible? I don't want my app to be bogged down by a queue of threads. How do I, say, cancel downloads on screen change?

I'm posting this question in Java since I think the process is the same.

Adam
  • 8,849
  • 16
  • 67
  • 131
  • 1
    What about using some of the following methods together for getting the visibility status of the screen that starts the thread, `onFocus(), onUnfocus(), isDisplayed(), isFocus(), onDisplay(), onUiEngineAttached(boolean attached), onUndisplay(), onUnfocus()`. Also `UiApplication.getUiApplication().getActiveScreen()` can be used to get currently displayed screen. – Rupak Jan 09 '12 at 05:25

2 Answers2

0

As @Rupak suggested you make method (using isDisplayed() for example):

boolean isScreenOnTop()

And pass it to the Thread (better over interface StopCondition.shouldStop()). And modify you downloading algorithm to next:

while(moreDataAvailable() && !topCondition.shouldStop()) {
   loadNextDataChunk();
}

if (!topCondition.shouldStop()) {
   notifyDataDownloaded();
}
Eugen Martynov
  • 19,888
  • 10
  • 61
  • 114
0

You can force your threads to close by keeping a public close() method inside your class that extends Thread:

private class MyConnectionThread extends Thread {
        /** determines whether the thread is runnuing or not. */
        private boolean alive = false;
        private HttpConnection hconn = null; // or whatever connection method you want to use ( SocketConnection etc.)
        private InputStream inputStream = null; // or DataInputStream etc...

        public MyConnectionThread() {
            alive = false;
            // ...
            // ...
        }

        public void run() {
            alive = true;
            try {
                String connection_parameter = ";deviceside=false"; // [For BlackBerry: ] this parameter is for connection using MDS, need to add different parameters for different connection methods.
                hconn = (HttpConnection) Connector.open("http://your_url.com"+connection_parameter);
                int response = hconn.getResponseCode();
                if (response == HttpConnection.HTTP_OK) {
                    inputStream = hconn.openInputStream();

                    // process the result here by reading the inputStream ...
                    // ...
                    // ...
                }

                inputStream.close();
                hconn.close();

            }catch(Exception excp) {
                // Handle Exceptions here...
            }catch (Throwable e) {
               // Exception in reading inputStream
            }finally{
                alive = false;
                this.interrupt();
            }
        }

        /**
         * Forces the connection to close anytime.
         */
        public void closeConnection() {
            alive = false;
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
                inputStream = null;
                if (hconn != null) {
                    hconn.close();
                }
                hconn = null;
                this.interrupt();
            } catch (Exception excp) {
                // Handle Exception here...
                System.out.println("Exception in closing HttpConnection: " + excp.toString());
            }
        }
    }

Now whenever you navigate to another screen you just need to call the MyConnectionThread.closeConnection() method to force-close this Thread.

See Also:

  1. how-to-abort-a-thread-in-a-fast-and-clean-way-in-java

  2. How can we kill the running thread in java?

  3. How to Stop a Thread or a Task

Hope these will be helpful for you.

Community
  • 1
  • 1
Tariq M Nasim
  • 1,278
  • 11
  • 24