Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1046033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:04:47+00:00 2026-05-16T16:04:47+00:00

I have got the new problem with opening and closing form in C#. My

  • 0

I have got the new problem with opening and closing form in C#.

My problem is how to dispose the form after closing .

here is my code :

Program.cs:

static class Program
{
    public static Timer timer;

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        timer = new Timer { Interval = 1000};
        timer.Start();

        Application.Run(new Form1());
    }
}

Form1.cs:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Form2 form = new Form2();
        form.ShowDialog();
       /// I've tried Dispose() method . but didn't work
    }
}

Form2.cs:

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }

    private void Form2_Load(object sender, EventArgs e)
    {
        Program.timer.Tick += timer_Tick;    
        Close();
        // I've tried Dispose() method instead of Close() but didn't work
    }

    private int count = 0; 
    void timer_Tick(object sender, EventArgs e)
    {
        count++;
        if (count == 5) MessageBox.Show("");
    }
}

Edited :
My question is : why the message box shows after 5 seconds when the form2 has closed!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T16:04:48+00:00Added an answer on May 16, 2026 at 4:04 pm

    This question turns out to be about Dispose.

    Firstly, Dispose has nothing to do with garbage collection. The following happens:

    1. You have a global Timer instance
    2. You create form2
    3. Form2 subscribes to the timer
    4. Form2 is Closed and/or Disposed
    5. The Timer event fires, increments the counter and shows a MessageBox
    6. The Timer event keeps firing until the App closes.

    The main point to understand is that Close/Dispose only change the status of the Form, they don’t (can’t) ‘delete’ the instance. So the (closed) form is there, the counter field is still there and the Event fires.


    OK, part 1:

    A using () {} block would be better but this should work:

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 form = new Form2();
            form.ShowDialog();
           /// I've tried Dispose() method . but didn't work
            form.Dispose(); // should work
        }
    

    If not, please describe "doesn’t work".


        private void Form2_Load(object sender, EventArgs e)
        {
            Program.timer.Tick += timer_Tick;    
            Close();
           /// I've tried Dispose() method instead of Close() . but didn't work
        }
    

    This is strange, but I’ll assume that it is artifical code for the question.

    Your global Program.Timer now stores a reference to your Form2 instance and will keep it from being collected. It does not prevent it from being Disposed/Close so your timer will keep firing for a Closed Form, and that will usually fail and cause other problems.

    1. Don’t do this (give Form2 it’s own timer)
    2. Use a FormClosed event to unsubscribe: Program.timer.Tick -= timer_Tick;
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have got below code which is working fine: public abstract class Beverage {
I have got the following code from here to read an Excel file using
I have got a new problem with my Android app. The SocketChannel tells me
My problem is this: I have got a view/action called new . In this
I have got a abstract class which is implementing 3 interfaces. public abstract class
I am new to XML & XSLT but have got this problem. I have
I have got a weird problem here, and more interesting that it works fine
Here is a description of my problem: I have got TabActivity consists of three
I have a problem in running the cron job. here are my code: <?php
I have got a C function in a static library, let's call it A,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.