Results 1 to 4 of 4

Thread: Why forms dosn't dispose?

  1. #1

    Thread Starter
    Lively Member neodatatype's Avatar
    Join Date
    Aug 2002
    Location
    Italy
    Posts
    103

    Why forms dosn't dispose?

    Hi all,

    Take this sample:

    Code:
    public class F : Form
    {
    private System.Windows.Forms.Button button1;
       F()
       {
          this.button1 = new Button();
          this.button1.Click += new System.EventHandler(this.button1_Click);
       }
    
       private void button1_Click(object sender, System.EventArgs e)
       {
           F f = new F ();
           f.Show();
       }
    
       [STAThread]
       static void Main() 
       {
           Forms.Application.Run(new F());
       }
      
    }
    Into button1_Click I create a new F() but this form does not get disposed as I aspected (f go out of scope!).

    The strange is that if I do this with some other class type (try with a new Class1) this get disposed!

    Why they have differents behaviors?

    Thanx!
    > NeoDataType.net <

    Try my Free .Net Reporting Tool!

  2. #2

  3. #3

    Thread Starter
    Lively Member neodatatype's Avatar
    Join Date
    Aug 2002
    Location
    Italy
    Posts
    103
    [QUOTE
    use the .Close() on the form first[/QUOTE]

    No, I'd like to know why forms doesn't dispose automatically as any other class.
    What's different?

    Cya
    > NeoDataType.net <

    Try my Free .Net Reporting Tool!

  4. #4
    Hyperactive Member Scott Penner's Avatar
    Join Date
    Dec 2000
    Location
    Mountain View
    Posts
    327
    This is dependent on the garbage collector. Forms might get automatically promoted to the next generation. You can force a full collection by typing gc.Collect to see if the collector clears the form. I also believe that if the form is still visible, it should be on the freachable list and therefore will not be collected. Of course you can force disposal using the IDispose interface. But again, if the form is visible then this may not be permitted.
    -scott
    he he he

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width