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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:33:41+00:00 2026-06-07T02:33:41+00:00

I have several objects in our framework, that by the requirement need to provide

  • 0

I have several objects in our framework, that by the requirement need to provide event ObjectTerminated. The user of the framework can subscribe to this event and clean-up some unmanaged stuff, that he is using. These objects are designed to exist the whole life time of the application, and I’m not controlling their life. You can think of them as an array of singletons.

I want to write code like this:

class SomeWorkflowControlObject
{
     public event EventHandler<> ObjectTerminated;

     ~SomeWorkflowControlObject()
     {
          if (ObjectTerminated != null) ObjectTerminated(this, null);         
     }
}

I am not sure, am I allowed to do that. What could go possibly wrong with such solution?

Updated:

What about Process.GetCurrentProcess().Exited ? Can I use it in such manner?

  • 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-07T02:33:42+00:00Added an answer on June 7, 2026 at 2:33 am

    You should not do this. Basically, destructors do not exist in C#. What you have written is a finalizer, and the only thing a finalizer should ever do is to free unmanaged resources.

    You are not allowed to access any other managed object at all, since the garbage collector might already have removed it. I do not think your null check is a sufficient guard against this situation; that object reference might still point to your (event) delegate, even if it’s already gone.

    So in short, don’t do this.

    Alternatives:

    1. Subscribe to the Application.ApplicationExit event if you have a Windows Forms application.

    2. You might want to consider implementing the IDisposable interface instead and then do something like this:

      public class SomethingVeryLongLived : IDisposable
      {
          …
      }
      
      …
      
      public static void Main()
      {
          using (var sth = new SomethingVeryLongLived(…))
          {
              Application.Run(new SomeForm(…));
          } // <-- at this point, foo.Dispose() is guaranteed to be called.
      }
      

      Take note that even in the case of using IDisposable, it’s probably not a good idea / design to trigger an event inside the object that is getting disposed, since disposed objects should no longer be accessed.

      For this very reason, I would recommend you do the following:

    3. Use a try…finally block:

      public static void Main()
      {
          var sth = new SomethingVeryLongLived(…);
          try
          {
              Application.Run(new SomeForm(…));
          }
          finally
          {
              SomethingVeryLongLived.Terminate();
          }
      }
      

      This would seem best to me because you are not abusing the IDisposable interface, and it’s very clear what the code does… there’s no hidden meaning.

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

Sidebar

Related Questions

I have several objects in my app that can become nil at some point
I have users that have several objects and can upload images for those objects.
I have several Entity Framework Code First DbContext objects that use a custom Initializer.
We have several Flash objects included in our project. We call each one a
I have several objects like this: I want to move type and value one
I have several methods that populate a SQLCommand objects parameter collection from an object
We have an application that uses several out of process COM objects for various
We have a C++ library that we provide to several different clients. Recently we
We have a page of search results which the user can hit in several
I currently have a map view that is populated with several objects following the

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.