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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T22:56:27+00:00 2026-06-18T22:56:27+00:00

There is code sample in the chapter about events in the book about c#:

  • 0

There is code sample in the chapter about events in the book about c#:

class CountDown
{
    private uint _seconds;
    public CountDown(uint seconds)
    {
        _seconds = seconds;
    }
    public void Start()
    {
        new Thread(() =>
        {
            uint n = _seconds;
            while (n > 0u)
            {
                var tick = Tick;              ///??????
                if (tick != null)
                    tick(n);

                Thread.Sleep(1000);
                n--;
            }
            var finished = Finished;          ///??????
            if (finished != null)
                finished();
        }).Start();
    }
}
    public event Action<uint> Tick;
    public event Action Finished;

What is the reason for creating local copy of event (tick and finished) and raising event via it? Is it common practice and have some sense? I tried but couldn’t get it from the book.

  • 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-18T22:56:28+00:00Added an answer on June 18, 2026 at 10:56 pm

    What is the reason for creating local copy of event (tick and finished) and raising event via it?

    It avoids the possibility of Tick becoming null after the nullity check but before the call. If you had:

    if (Tick != null)
    {
        Tick(n);
    }
    

    … and the final listener was unsubscribed when you’d already got into “if” body, you’d get a NullReferenceException.

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

Sidebar

Related Questions

There is a sample code which works in a multithreading environment: void CSampleClass::Stop(void) {
I followed code sample in Steve Sanderson's MVC3 book , there is a piece
Is there a sample code available online to get WinRT to determine if its
There is a sample code of apple named reachability which tells us the network
Is there a framework or other sample code for doing barcode recognition (create and
Does anyone know if there is an sample code for using Visual Basic to
Is there an Android equivalent of didUpdateHeading (from iOS) ? Any sample code to
From the play2's doc: http://www.playframework.org/documentation/2.0/ScalaForms , there is a sample code: val loginForm =
Is there any utility (or sample source code) that truncates HTML (for preview) in
I am going through a java 6 book. A sample code snippet is given

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.