5 Replies Latest reply: Jun 17, 2011 11:53 AM by 869590 RSS

    Stopping a Thread when downloading

    869590
      Hi everyone!

      I'm develop a app to connect to a FTP Server and dowload and upload files. When I click on Cancel button, the app will still running but the download will stop.

      So, my problem is: How can I stop a Thread when i'm donwloading a file?

      I'm using Commons Net 3.0.1 to connect from a FTP. I have tried logout(), disconnect(), abort()... Into Threads I have tried interrupt(), yield(), stop(), destroy()... But not works!

      The "safe way to stop a Thread" don't work for me, becouse I'm not using a While but yes this line code to download the file:

      ftp.retrieveFile(TARGET, SOURCE);

      When I click on cancel button the donwload still running in background.

      PS: Sorry for my english :)
        • 1. Re: Stopping a Thread when downloading
          802316
          This is a feature either the library supports or it doesn't. There is not much you can do externally.

          The only safe way to cancel a thread is to run a separate process which you can kill and clear up any temporary files.

          I suggest you are the developers of the library how it can be done or ask them to support it.
          If it doesn't say how to do this in their documentation, you could ask them to make it clear that either it can't be done or how to do it with an example.

          They might tell you when in the documentation it is covered already. (assuming it is)
          • 2. Re: Stopping a Thread when downloading
            EJP
            When I click on Cancel button, the app will still running but the download will stop.

            When I click on cancel button the donwload still running in background.
            Which is it? You can't have it both ways.
            • 3. Re: Stopping a Thread when downloading
              869590
              As I said, i need stop the download when the cancel button is clicked, but still running in background. He only stop when app is closed :(
              • 4. Re: Stopping a Thread when downloading
                869590
                Peter, I have tried delete the temporary files but they are protected when downloading.

                As you suggest I'll ask a solution for the developers.

                Edited by: 866587 on 17/06/2011 06:08
                • 5. Re: Stopping a Thread when downloading
                  869590
                  Problem solved! Using the method retrieveFileStream() and the famous "the safe way to stop a Thread" I can stop it without need to close my app!

                  The code was (adapted):
                  OutputStream os;
                  InputStream is;
                  
                  localDownload = new FileOutputStream(new File(LOCAL, NOME_ARQUIVO));
                  os = localDownload;
                  
                  is = ftp.retrieveFileStream(LOCAL_FTP+NOME_ARQUIVO_FTP);
                  
                  if(is != null){
                       int bytesRead = -1;
                       final byte[] buffer = new byte[4096];
                       while ((bytesRead = is.read(buffer)) != -1){
                            if(cancelado == false)
                                 os.write(buffer, 0, bytesRead);
                            else
                                 cancelado = false;
                       }
                  }
                  is.close();
                  ftp.completePendingCommand();
                  PS: The method completePendingCommand() is fundamental for the download can be stoped.

                  Again: sorry for my english :)

                  Edited by: 866587 on 17/06/2011 09:53