Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 766
  • Last Modified:

How to pause applet closure in order to save data

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
apatia
Asked:
apatia
  • 4
  • 2
  • 2
  • +2
1 Solution
 
objectsCommented:
Your code will have to run in stop() or destroy(), I don't know of any other option.
0
 
Prasanna_HebbarCommented:
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
 
omry_yCommented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
dvijiCommented:
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
 
dvijiCommented:
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
 
apatiaAuthor Commented:
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
 
omry_yCommented:
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
 
omry_yCommented:
I fail to understand how the code in the accepted answer helps preventing the JVM from exiting.
0
 
Prasanna_HebbarCommented:
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
 
omry_yCommented:
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now