Microsoft KB Archive/318175

= PRB: Browser Hangs When You Call dispose on AWTModal FileDialog Object =

Article ID: 318175

Article Last Modified on 10/20/2003

-

APPLIES TO


 * Microsoft virtual machine 38xx Series, when used with:
 * Microsoft Windows XP Professional

-



This article was previously published under Q318175



SYMPTOMS
If you call the dispose function on a Java FileDialog object in Windows XP, the browser may stop responding (hang) at random times. For example, suppose that you create a simple Java applet with a button that opens a File dialog box when you click it. If you repeatedly click the button to open the File dialog box and then click Cancel to close the File dialog box, your browser may hang.



CAUSE
This problem occurs if the current thread that starts the File dialog box is the event dispatch thread.



RESOLUTION
Create a new thread to display the File dialog box. Do not use the default event dispatch thread to display the File dialog box. Refer to the &quot;Steps to Resolve the Problem&quot; portion of the &quot;More Information&quot; section for implementation details.



Steps to Reproduce Behavior
  Create the java class AWTApplet.java as follows: import java.applet.Applet; import java.awt.*; import java.awt.event.*;

public class AWTApplet extends Applet implements ActionListener {   Button button   = new Button(&quot;Call FileDialog&quot;); public void init {       // a button panel Panel bpanel = new Panel; bpanel.add(button);

GridLayout gridLayout = new GridLayout; this.setLayout(gridLayout); this.add(bpanel);

// Add the ActionListener. button.addActionListener(this); }   public void actionPerformed(ActionEvent ae) {       FileDialog dlgFile; dlgFile = new FileDialog((Frame)getParent,&quot;test&quot;); dlgFile.show; // The following call to the dispose function causes the browser to hang. dlgFile.dispose; } } This code spawns a new thread to open the File dialog box.  To compile the java class, type jvc awtapplet.java at the command line. You can run the preceding code if it originates from a trusted source. To access files from the File dialog box, Java comes out of the Java sandbox. (The Java sandbox is the security scheme for Java programs.) You can access files with Java if you put the class files in a signed .cab file, which is noted in the &quot;References&quot; section.

The following command-line commands use tools from the Microsoft Software Development Kit (SDK) for Java to compile the Java source file and to generate a signed .cab file that uses the Microsoft Test Certificate:

'''jvc awtapplet.java

cabarc n myapplet.cab awtapplet.class setreg 1 true

makecert -sk mytestkey -n &quot;cn=mytestkey&quot; mytestkey.cer

cert2spc mytestkey.cer mytestkey.spc

signcode -j javasign.dll -jp low -spc mytestkey.spc -k mytestkey

myapplet.cab

chkjava myapplet.cab'''

  Create a Hypertext Markup Language (HTML) file that appears as follows:    </li> Load the HTML page in a Web browser, click Call FileDialog, and then click Cancel in the File dialog box.</li> Repeat step five until the browser hangs.</li></ol>

Steps to Resolve Problem
<ol>  Implement the AWTApplet class as follows: import java.applet.Applet; import java.awt.*; import java.awt.event.*;

public class AWTApplet extends Applet implements ActionListener, Runnable {   Button button   = new Button(&quot;Call FileDialog&quot;); public void init {       // a button panel Panel bpanel = new Panel; bpanel.add(button);

GridLayout gridLayout = new GridLayout; this.setLayout(gridLayout); this.add(bpanel);

// Add the ActionListener. button.addActionListener(this); }   public void actionPerformed(ActionEvent ae) {       new Thread(this).start; }   public void run {       FileDialog dlgFile; dlgFile = new FileDialog((Frame)getParent,&quot;test&quot;); dlgFile.show; dlgFile.dispose; }

} This code spawns a new thread to open the File dialog box.

</li> Repeat steps 2 through 5 from the &quot;Steps to Reproduce Behavior&quot; section.</li></ol>

<div class="references_section">