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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T21:04:40+00:00 2026-05-10T21:04:40+00:00

I have a static class that I would like to raise an event as

  • 0

I have a static class that I would like to raise an event as part of a try catch block within a static method of that class.

For example in this method I would like to raise a custom event in the catch.

public static void saveMyMessage(String message) {     try     {         //Do Database stuff     }     catch (Exception e)         {               //Raise custom event here         } } 

Thank you.

  • 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. 2026-05-10T21:04:41+00:00Added an answer on May 10, 2026 at 9:04 pm

    Important: be very careful about subscribing to a static event from instances. Static-to-static is fine, but a subscription from a static event to an instance handler is a great (read: very dangerous) way to keep that instance alive forever. GC will see the link, and will not collect the instance unless you unsubscribe (or use something like a WeakReference).

    The pattern for creating static events is the same as instance events, just with static:

    public static event EventHandler SomeEvent; 

    To make life easier (re null checking), a useful trick here is to add a trivial handler:

    public static event EventHandler SomeEvent = delegate {}; 

    Then you can simply invoke it without the null-check:

    SomeEvent(null, EventArgs.Empty); 

    Note that because delegate instances are immutable, and de-referencing is thread-safe, there is never a race condition here, and no need to lock… who-ever is subscribed when we de-reference gets invoked.

    (adjust for your own event-args etc). This trick applies equally to instance events.

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

Sidebar

Related Questions

Suppose that I have a Java class with a static method, like so: class
I have a class that only has static members. I would like to register
If you have a class member that is static and public . Would you
We have a static method in a utility class that will download a file
I have a base class that has a private static member: class Base {
Is there a way to have a mutable static variable in F# class that
I have a helper class that is just a bunch of static methods and
Very simply put: I have a class that consists mostly of static public members,
I have a co-worker that swears by //in a singleton Constants class public static
Let's say that I have a class Foo: public class Foo { public static

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.