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

  • Home
  • SEARCH
  • 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 9284391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T18:53:23+00:00 2026-06-18T18:53:23+00:00

This may seem like a silly question, but I just want to make sure

  • 0

This may seem like a silly question, but I just want to make sure I have it right. My main form is not visible most of the time. To open it I have a NotifyIcon. One of the menu options is Exit Application. I also have a few static global variables that need to be disposed of before the application closes. so in program.cs i have this.

    [STAThread]
    static void Main()
    {
        InitializeApplication();
        InitializeMainForm();
        Application.Run(main);
    }
    private static void InitializeApplication()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.ApplicationExit += Application_ApplicationExit;
    }
    private static void InitializeMainForm()
    {
        main = new AssignButtonForm();
        main.FormClosing += main_FormClosing;
        Globals.StartNotify();
    }

    static void main_FormClosing(object sender, FormClosingEventArgs e)
    {
        var dlg = MessageBox.Show("Turn off Application?", "Exit?", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
        if (dlg == DialogResult.OK)
        {
            Globals.notifyIcon1.Dispose();
            Application.Exit();
        }
        else
        {
            e.Cancel = true;
        }
    }

So what i’m hoping is that this is the correct way to call that.

    private void exitToolStripMenuItem_Click(object sender, EventArgs e)
    {
        this.Hide();
        Application.OpenForms[0].Close();
    }

is that correct? or is there a better way?

EDIT

ok so one of my classes that has its own Dispose method

public class KeymonNotifyIcon : IDisposable
{
    public KeymonNotifyIcon()
    {
        InitializeComponent();
        keymonMenuStrip.SetupKeysSelected += OnSetupKeysSelected;
    }
    ~KeymonNotifyIcon()
    {
        Dispose();
    }
    public void Dispose()
    {
        if (notifyIcon1 != null)
            notifyIcon1.Dispose();

        if (keymonMenuStrip != null)
            keymonMenuStrip.Dispose();
    }
}

The global class

public static class Globals
{
    public static TraceSource trace = new TraceSource("Keymon");
    public static KeymonNotifyIcon notifyIcon1;
    public static void StartNotify()
    {
        notifyIcon1 = new KeymonNotifyIcon();
    }
}
  • 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-06-18T18:53:24+00:00Added an answer on June 18, 2026 at 6:53 pm

    If your program just shuts down, objects that have not been garbage collected will have their finalizers run. It is the recommended pattern that objects implementing IDisposable have a finalizer to ensure that IDisposable is run. To the best of my knowledge, .NET BCL classes always follow that pattern. However, your own or third-party/open source components might not follow that pattern.

    Note: The linked pattern on MSDN does not call GC.SuppressFinalize. Check out how to use it to reduce the GC overhead.

    Implementing IDisposable alone isn’t sufficient to ensure that the object is properly disposed.

    If you have statically referenced objects that implement IDisposable, it is a more solid solution to explicitly call IDisposable.Dispose() on them from an application shut down event.

    EDIT

    Your Dispose implementation will cause Dispose() to be called twice on the owned objects because:

    if (notifyIcon1 != null)
            notifyIcon1.Dispose();
    

    does not set notifyIcon1 to null, and the finalizer unconditionally calls Dispose() again.

    Additionally, you will always cause the finalizer to run (which makes GC more expensive for that class) because you do not call GC.SuppressFinalize() in Dispose().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This may seem like a silly question but I can't figure it out. let's
Now this may seem like a silly question, but I need to know how
this may seem like a very simple question, but I want to dump some
This may seem like a silly question, but what are the functional differences, if
This may seem like a silly question, but we are having an issue debugging
This may seem like a stupid question but I just started learning javascript about
This may seem like a very simple question, but I have been struggling with
Im new to rails so this may seem like a silly question but why
This may seem like a silly question, but I thought I'd ask it anyway
This may seem like a really easy question to answer, but I'm just 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.