Solved

How to pause applet closure in order to save data

Posted on 2001-08-09
10
761 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
LVL 1

Accepted Solution

by:
dviji earned 300 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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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:
Suggested Courses

623 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