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 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

I have a static class which contains a RoutedUICommand that I would like to
I have a static class that looks like this: namespace Argus { static class
Suppose I have a static method of my class that returns an object of
I have a phrase counter function as part of a class that I would
I have a C++ static class with a method that creates an object. I
What I would like to do is have a static factory function that you
I would like to know about static (class) field representation within PHP interpreter. For
I want to have a static class that would hold different kinds of data
I have a static class that I use as my Data Utils for my
So I have a static class that is supposed to be used as 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.