Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to pause applet closure in order to save data

Posted on 2001-08-09
10
Medium Priority
?
765 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
ID: 6370837
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
ID: 6370980
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
ID: 6371379
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!

 
LVL 1

Accepted Solution

by:
dviji earned 1200 total points
ID: 6373096
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
ID: 6373105
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
 

Author Comment

by:apatia
ID: 6374062
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
ID: 6374691
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
ID: 6381872
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
ID: 6384067
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
ID: 6384124
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This video teaches viewers about errors in exception handling.
Suggested Courses

916 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