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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:32:17+00:00 2026-05-29T05:32:17+00:00

this code that adds registers new EventHandler(s) for an event named as NewMail (the

  • 0

this code that adds registers new EventHandler(s) for an event named as NewMail (the eventargs class is named NewMailEventArgs.

// A PUBLIC add_xxx method (xxx is the event name)
// Allows methods to register interest in the event.
public void add_NewMail(EventHandler<NewMailEventArgs> value) {
  // The loop and the call to CompareExchange is all just a fancy way
  // of adding a delegate to the event in a thread-safe way.
  EventHandler<NewMailEventArgs> prevHandler;
  EventHandler<NewMailEventArgs> newMail = this.NewMail;
  do {
     prevHandler = newMail;
     EventHandler<NewMailEventArgs> newHandler = (EventHandler<NewMailEventArgs>)Delegate.Combine(prevHandler, value);
     newMail = Interlocked.CompareExchange<EventHandler<NewMailEventArgs>>(ref this.NewMail, newHandler, prevHandler);
    }
  while(newMail != prevHandler);
}

(source : CLR via C#, chapter 11 Events)
What I don’t understand is the do part, first we are assigning newMail to prevHandler, then newMail is changed (in CompareExchange) to newHandler? Then we are checking if newMail != prevHandler?
I am really kinda confused. Can any one help me understand what exactly is going on in here, specially in the do loop?

  • 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-29T05:32:18+00:00Added an answer on May 29, 2026 at 5:32 am

    As the comment says, it’s to provide a kind of safety way to deal with events in multi-threaded environment. This is pretty tricky actually, but here’s how it works:

    • What does Interlocked.CompareExchange is : if prevHandler == this.NewMail, then this.NewMail = newHandler

    • The whole operation (compare + affect) is atomic, i.e. done in one time (cannot be stopped in the middle of the operation by another thread).

    • If NewMail is not equal to prevHandler, then it means another thread already ran the same piece of code and already modified the event handler. So we won’t do anything here and we’ll loop and try again, hoping next time there is no other thread that already registered an event handler (next time we’ll re-read the event handler; the operation done by the other thread will be now taken into account).

    See also this useful thread.

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

Sidebar

Related Questions

I have this code that runs but never stops. class A { public static
This jQuery 1.3.2 code adds an element to the page, registers a click event,
So I have this code here, that adds my NamePrinter runnable class as ExecutorService
I have this register that registers all the objects I need: public static class
I have this code that suppose to place the marker by the latlng public
I have following code that adds dynamic LinkButton on the click event of a
I have this PHP code that adds a set of nodes to an XML
I have this piece of code that adds a BOM UTF-8 to an outputstream
I have a JavaScript code that adds new XHTML to already existing page (DOM
In this code that uses the mongodb-native driver I'd like to increase the value

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.