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 3332068
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:38:56+00:00 2026-05-17T23:38:56+00:00

I am working on a C# application that has multiple forms. When I open

  • 0

I am working on a C# application that has multiple forms.
When I open one of the forms I add an event listener like this: SomeClass.MotionCompleted += new EventHandler(HandlerMethod); . The MotionCompleted event is a static event.

I have noticed that after closing this form the HandlerMethod still gets called when the event occurs which then causes an exception because it tries to update something on the form which doesn’t exist anymore.

How can the event listener exist and respond to the event even though the form doesn’t exist anymore? Once form.Close() or this.Close() is called shouldn’t that automatically unhook the event listeners so that they do not get called anymore?

  • 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-17T23:38:56+00:00Added an answer on May 17, 2026 at 11:38 pm

    That’s the evil of static events! 🙂 You have a managed leak there.

    Override the OnClosing of your form and deregister you handler:

    protected override void OnClosing(CancelEventArgs e) {
        SomeClass.MotionCompleted -= new EventHandler(HandlerMethod);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a Windows Forms (.NET 3.5) application that has a built-in exception
I'm working on a solution that has two projects: One ASP.NET web application, and
An application that has been working well for months has stopped picking up the
I'm working on a PyGTK/glade application that currently has 16 windows/dialogs and is about
I am working on a WPF application which has a treeview that represents an
I have recently started working on a legacy application that has most of its
I've been working on an iPad application that has about 15 view controllers. Some
We're working on an application that displays information through a Direct3D visualisation. A late
I am working on an application that installs a system wide keyboard hook. I
I am working on an application that is about 250,000 lines of code. I'm

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.