Re: a Timer in a URLConnection



Hi everybody,

Now i have something like a solution, but I need some help here. The only thing that doesn't work very well is the setDaemon() method. I don't want to use it because my program can still running if there are other active threads (which is possible) so it will take part of my Internet connection.

I don't know how to avoid this step, because the line that is driving me crazy is

length = connection.getContentLength();				

somehow, the connection is still open even if I kill the thread   t=null
Do you have an idea in order to avoid this problem?

thanks for your help,

Marcelo

public class TimerTemp{
public Timer timer;
public int length = -1; public boolean isFinished = false;

URLConnection connection;
Thread t;
public TimerTemp(final URL urlObject){


t = new Thread(){
public void run(){
try{
connection = urlObject.openConnection();
connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible;MSIE 5.5; Windows NT 5.0;H010818)" );
//Connect to the server connection.connect();
length = connection.getContentLength();
}catch(IOException e){
e.printStackTrace();
}
}
};
//TODO: Not a good implementation
t.setDaemon(true);
t.start();

timer = new Timer(true);
timer.schedule(new temp(), 2000);
}

private class temp extends TimerTask{
public void run(){
if(length == -1)
System.out.println("Time's up");
else
System.out.println("length "+length);

timer.cancel();
connection = null;
System.out.println("alive:"+t.isAlive()+t.getState());
t = null;
isFinished= true; }
}
}
.




Relevant Pages

  • Re: subclass JPasswordField
    ... for (like how to add a document change listener to the PlainDocument ... inside the JPasswordField & how to set the timer and all). ... Google "java timer tutorial". ... public void insertUpdate{ ...
    (comp.lang.java.programmer)
  • Re: Can I implement a timer within a thread without a blocking wait?
    ... I think that a periodically running process may consume more server ... I'm trying to implement a multithread server program on Linux. ... I'll close the connection. ... But I can't implement timer signal handler for each thread. ...
    (comp.programming.threads)
  • Re: RAS connection and Socket status
    ... You don't need a timer to check the current RAS status. ... recv) to ensure that status messages can be processed. ... RasGetConnectStatus to check it during the connection process ...
    (microsoft.public.pocketpc.developer)
  • Re: Pocket outlook - AppointmentsCollection class: ListChanged event
    ... void POOMHelper_OutlookItemChanged(PIMNotificationMessage notification, PIMItemType itemType, int itemOID, int databaseOID) ... static Timer timer= new Timer; ... Thread timerThread = timer.Start; ... public void SleepIntermittently ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: Can I implement a timer within a thread without a blocking wait?
    ... I'll close the connection. ... But I can't implement timer signal handler for each thread. ... When you want to timeout a connection, add a timer to the linked ... Also clear the flag anytime the connection is active. ...
    (comp.programming.threads)