IE does not call stop() on applet

K

karambol

Hi,

There is a situation:
An applet in start() method creates a socket and two threads to read from
and write to it.
When I open the page, let the applet initialize and then close, everything
is ok.

The problem comes up, when I quickly click "back" in IE just after the
applet starts initializing (sometime about when the Java icon is shown and
the applet is not yet initialised, and its controls are not yet visible).

The problem is, that IE is not calling stop(), as supposed, so the applet
keeps initializing, although it's not visible and I'm watching another site
(due to clicking "back").

Question:
How to stop the threads and close the socket, when user quickly clicks
"back", and applet keeps initializing itself?
 
L

Liz

karambol said:
Hi,

There is a situation:
An applet in start() method creates a socket and two threads to read from
and write to it.
When I open the page, let the applet initialize and then close, everything
is ok.

The problem comes up, when I quickly click "back" in IE just after the
applet starts initializing (sometime about when the Java icon is shown and
the applet is not yet initialised, and its controls are not yet visible).

The problem is, that IE is not calling stop(), as supposed, so the applet
keeps initializing, although it's not visible and I'm watching another site
(due to clicking "back").

Question:
How to stop the threads and close the socket, when user quickly clicks
"back", and applet keeps initializing itself?

It is my understanding that stop() is called when the applet completely
leaves the screen.
 
K

karambol

It is my understanding that stop() is called when the applet completely
leaves the screen.


Well, it does. If I click "back" after, say, 3 seconds or later, everything
works ok. But if I click "back" "too soon" (meaning: when the start() method
has not finished yet) the stop() is not called.

Seems like IE only calls stop() when the start() is over, but I think there
is something else. I tried creating a thread todo initialisation, so the
start() method looks like this:

InitThread it = new InitThread();
it.start();

so, how long can that execute?! All initialisation is done in another thread
now, and stop() is still not called if I exit the page too quickly.

One more thing: stop() is called when i exit the browser. This is
understandable, but why not when i leave the page (too quickly)?
 
L

Liz

karambol said:
Well, it does. If I click "back" after, say, 3 seconds or later, everything
works ok. But if I click "back" "too soon" (meaning: when the start() method
has not finished yet) the stop() is not called.

Seems like IE only calls stop() when the start() is over, but I think there
is something else. I tried creating a thread todo initialisation, so the
start() method looks like this:

InitThread it = new InitThread();
it.start();

so, how long can that execute?! All initialisation is done in another thread
now, and stop() is still not called if I exit the page too quickly.

One more thing: stop() is called when i exit the browser. This is
understandable, but why not when i leave the page (too quickly)?

Have you read the documentation? From the tutorial:
---
Does the applet stop running when it's offscreen?

Most applets should not use CPU resources when the browser is iconified or
is displaying a page that doesn't contain the applet. If your applet code
doesn't launch any threads explicitly, then you're OK.
If your applet code launches any threads, then unless you have a really
good excuse not to, you should implement the stop method so that it stops
and destroys (by setting to null) the threads you launched.
 
R

Roedy Green

so, how long can that execute?! All initialisation is done in another thread
now, and stop() is still not called if I exit the page too quickly.

I find it highly unlikely the browser would abort any of the 4 methods
init, start, stop and destroy part way through. I think you can prove
this to yourself by putting a debug dump line at the head and tail of
each of those methods.

Applets are a bit hazy on who calls validate and repaint when.

That may be where you are getting hammered.
 
K

karambol

I find it highly unlikely the browser would abort any of the 4 methods
init, start, stop and destroy part way through. I think you can prove
this to yourself by putting a debug dump line at the head and tail of
each of those methods.

I implemented stop() and destroy() and even finalize() methods in applet and
threads. Those methods are not called if I exit the page too soon.

There is more.. I put some debugging via System.out.print AND the Java
console shows, that the applet recieves data from the server, and the
threads are working, even though I clicked "back", and the applet is not
even visible!

It looks like the applet keeps initializing when the page is exited in a
moment, when applet is not yet fully initialized. Or, that IE calls stop()
after some period of time, which in this case is too long and IE can't call
it because it's not on the "applet page" any more.
 
K

karambol

If your applet code launches any threads, then unless you have a really
good excuse not to, you should implement the stop method so that it stops
and destroys (by setting to null) the threads you launched.

I implemented stop() and destroy(). The problem is, they are not being
called, if i exit the page too soon (meaning: when the applet is being
initialized or started).

Try this:

1. Create an applet with:

start() {
//some long initialization code here
SomeThread t = new SomeThread();
t.start();
}

2. In SomeThread.run put some debug info like:

System.out.print("STILL HERE");
sleep(100);

3. Compile it, put on page, open in IE and move to another page just after
opening the "Applet page", when you see this "java-style" gray rectangle
instead of your applet content. The applet will keep initializing, the
thread will start and be working, and the stop() will not be called. At
least, this is what happened in my case.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top