Solved

How to pause applet closure in order to save data

Posted on 2001-08-09
10
752 Views
Last Modified: 2008-02-01
I am running a java applet in netscape 4.x. This applet is not embedded in a webpage. It opens in its own window.
If user select file -> exit in applet, I can programatically save data to database before closing the applet.

If the user either:

A:  clicks the 'X' in the upper right hand of the window
B: hits alt-f4 on the key or right clicks
C:  right-clicks on the window's button on the task bar and selects close

I cannot save application data before the applet closes.
The code necessary to save this data (to a database)is to extensive to execute completely in the stop() or destroy() methods.

Is there anyway to pause the applet shutdown in order to save data?

If a user tries to close ms word by any method other than force quitting w/o saving changes, word pauses the shutdown process to prompt user to save changes. I would like to do the same thing - except prompting the user - using a java applet in a netscape browser.
0
Comment
Question by:apatia
  • 4
  • 2
  • 2
  • +2
10 Comments
 
LVL 92

Expert Comment

by:objects
Comment Utility
Your code will have to run in stop() or destroy(), I don't know of any other option.
0
 
LVL 2

Expert Comment

by:Prasanna_Hebbar
Comment Utility
I think it should be possible to do that using applet to javascript communication. (Using livewire). Write a method in the applet which returns the status whether the data has been saved. (Something like isDataSaved()).

You can write a javascript which will be called when the window is closed. This method will talk to the applet and checks if the data is saved. If the data is not saved, then does not close the window.

This is a vague solution. I remember reading about applet communication using livewire. I am not really confident about this solution. So you might want to study more about that before continuing.
0
 
LVL 4

Expert Comment

by:omry_y
Comment Utility
as objects says, your code will have to execute in the destroy method (stop is a bad choice here, because netscape got a nasty habbit of stopping and starting a java applet when netscape is resized)
also, remember that you can`t do long tasks in the destroy method.
the JVM will just close on you.
also, know that explorer and netscape have different policies about how to handle applets that do long tasks on destroy, but I dont remember who is nicer.
I once tested it in the following way :
in the applet destroy, put a for loop that iterates 50 times, each time sleep 500 ms and do a Toolkit.beep().
now just see how many beeps your applet emits when you close the browser.
0
 
LVL 1

Accepted Solution

by:
dviji earned 300 total points
Comment Utility
Hi apatia,
         If you call  your save routine in destroy that will work. But instead of driectly calling that if you create one thread and write your save routine it will be best. Very important is you have to do 'join()'..

i.e
public void init()
{
   ///
   MyThread thread = new MyThread();
}

public void destroy()
{
 thread.start();
 try
 {
   thr.join();
 }
 catch(Exception e)
 {
   e.printStackTrace();
 }

}
...
public class MyThread extends Thread
{
public void run()

}
[Need for join()
i.e once if the detroy method is called then first all the threads created for that window will die first.. if you do join() before that the user thread will run..
]

 I was tried to print simple text in console.. without thread it was printed like 150 times.. but with the thread it was printed more than 10000 times. And I also tried with sockets (juse created one small server) and it was works fine (in serverside if I write it into the file 13,550 times it was printed and with the console 7850 times). !!!!.

 I hope this is best way.. If you wants to store very small amount of data then it will be fine otherwise ... ;-(((. But these are all not 100% safe.
0
 
LVL 1

Expert Comment

by:dviji
Comment Utility
Hi apatia,
         If you call  your save routine in destroy that will work. But instead of driectly calling that if you create one thread and write your save routine it will be best. Very important is you have to do 'join()'..

i.e
public class MyApplet extends Applet
{
public void init()
{
   ///
   MyThread thread = new MyThread();
}

public void destroy()
{
 thread.start();
 try
 {
   thread.join();
 }
 catch(Exception e)
 {
   e.printStackTrace();
 }

}
...
class MyThread extends Thread
{
  public void run()
  {
    save();
  }

}
}
[Need for join()
i.e once if the detroy method is called then first all the threads created for that window will die first.. if you do join() before that the user thread will run..
]

 I was tried to print simple text in console.. without thread it was printed like 150 times.. but with the thread it was printed more than 10000 times. And I also tried with sockets (juse created one small server) and it was works fine (in serverside if I write it into the file 13,550 times it was printed and with the console 7850 times). !!!!.

 I hope this is best way.. If you wants to store very small amount of data then it will be fine otherwise ... ;-(((. But these are all not 100% safe.

Best of luck... ;-)))

....dviji
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:apatia
Comment Utility
Thanks everyone for your comments. I'll try all the suggestions and let you know how it goes.

As for putting the save code in the destroy() method, it just doesn't work. There's not enough time.
0
 
LVL 4

Expert Comment

by:omry_y
Comment Utility
there is nothing you can do.
you may not do any time consuming operations when the JVM is beeing closed.
just do it earlier.
0
 
LVL 4

Expert Comment

by:omry_y
Comment Utility
I fail to understand how the code in the accepted answer helps preventing the JVM from exiting.
0
 
LVL 2

Expert Comment

by:Prasanna_Hebbar
Comment Utility
Even I didn't understand why another thread is created at the end? It is as good as doing it in the main thread. dviji, would you like to give explanation for that?

omry_y, I don't think jvm will be closed when the browser is closed. My understanding is that all the instances of browser share the same jvm. So closing one window should not shut down the JVM.

Thanks
0
 
LVL 4

Expert Comment

by:omry_y
Comment Utility
On explorer, each explorer process have its own JVM.
an explorer process is created when you click the explorer icon.
if you create a new window through the explorer UI (open location, new window etc), a new process is NOT created, and thus the two windows indeed share the JVM.
the JVM is closed when the explorer process is closed, and this happens when the last explorer window of that process is closed.
but in this scenario, you must assume than when the user will close the browser, the JVM will be closed too.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
sumHeights2  challenge 7 75
Safari, Firefox, IE, Java, Plugin, Java Security error 13 39
mapBully challenge 6 88
word0 challenge 4 52
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now