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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T23:13:39+00:00 2026-06-07T23:13:39+00:00

Using my cusom EventArgs such as : public event EventHandler<MyEventArgs> SampleEvent; from msdn e.g

  • 0

Using my cusom EventArgs such as :

public event EventHandler<MyEventArgs> SampleEvent;

from msdn e.g :

public class HasEvent
{
// Declare an event of delegate type EventHandler of 
// MyEventArgs.

    public event EventHandler<MyEventArgs> SampleEvent;

    public void DemoEvent(string val)
    {
    // Copy to a temporary variable to be thread-safe.
        EventHandler<MyEventArgs> temp = SampleEvent;
        if (temp != null)
            temp(this, new MyEventArgs(val));
    }
}

I have 2 questions:

1)
looking at the marked code :

enter image description here

I don’t see a reason why it should be copied to another param ( regarding threads)

since we have the event keyowrd , no one can touch its invocation list ( no outsider code to the class I mean)

2) If I’m not mistaken, the DemoEvent function should be virtual, so it can be overridden in sub classes… (I’m sure I’ve seen it somewhere)

the strange thing is that resharper also won’t add virtual :

so if I have this code:

enter image description here

it suggests me :

enter image description here

and when I press it :

enter image description here

so again my 2 questions :

1) What is the scenario which this line EventHandler<MyEventArgs> temp = SampleEvent; will solve , regarding thread safty?

2) Shouldn’t the function be virtual? ( I’m sure I’ve seen this pattern with virtual)

  • 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-07T23:13:42+00:00Added an answer on June 7, 2026 at 11:13 pm

    what is the scenario which this line EventHandler temp = SampleEvent; will solve , regarding thread safty?

    Imagine you do this:

    if (SampleEvent != null)
        SampleEvent(this, new MyEventArgs());
    

    If another thread will detach the event handler after the if but before the invocation then you’ll try to call a null delegate (and you’ll get an exception).

    shouldn’t the function be virtual ? ( im sure ive seen this pattern with virtual)

    Yes, if the class is not sealed then you should mark that function virtual (it’s not mandatory but it’s a well accepted pattern).

    EDIT

    Time  Thread 1                                      Thread 2
    1                                                   obj.SampleEvent += MyHandler;
    2      if (SampleEvent != null)                     
    3      {                                            obj.SampleEvent -= MyHandler;
    4          SampleEvent(this, new MyEventArgs());
    5      }
    

    In this case at time 4 you’ll call a null delegate and it’ll throw a NullReferenceException. Now look this code:

    Time  Thread 1                                      Thread 2
    1                                                   obj.SampleEvent += MyHandler;
    2      var sampleEvent = SampleEvent;
    3      if (sampleEvent != null)                     
    4      {                                            obj.SampleEvent -= MyHandler;
    5          sampleEvent(this, new MyEventArgs());
    6      }
    

    Now at time 5 you call sampleEvent and it holds the old content of SampleEvent, in this case it won’t throw any exception (but it’ll call MyHandler even if it has been removed by the second thread).

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

Sidebar

Related Questions

I'm using custom class that subclass from UILabel. It works fine unless I change
I'm wondering why should I use a class that inherits from the EventArgs class
Im using WPF which has a Storyboard class that has a Completed event. I
I have a custom PageBase class that I am using for all of the
I'm trying to create a custom control and need to raise an event from
I have made a custom textbox that inherits from textbox. using System.Drawing; using System;
I'm using ListBox with custom ItemsSource: this.ListOfPersonsListBox.ItemsSource = (List<Person>)ListOfPersons.AllPersons; ListOfPersons is a static class
Can I update only selected variables within a custom EventArgs class or do I
I'm trying to programatically create a SharePoint site from a custom template using ASPX
When using custom assemblies in a visual studio project. How does one check in

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.