delegates and events
-
// Holds the arguments for the StatusChanged event
public class StatusChangedEventArgs : EventArgs
{
// The argument we're interested in is a message describing the event private string EventMsg; // Property for retrieving and setting the event message public string EventMessage { get { return EventMsg; } set { EventMsg = value; } } // Constructor for setting the event message public StatusChangedEventArgs(string strEventMsg) { EventMsg = strEventMsg; }
}
// This delegate is needed to specify the parameters we're passing with our event
public delegate void StatusChangedEventHandler(object sender, StatusChangedEventArgs e);public static event StatusChangedEventHandler StatusChanged;
Okay, so in the above code, we created a delegate and a custom event.// This is called when we want to raise the StatusChanged event public static void OnStatusChanged(StatusChangedEventArgs e) { **StatusChangedEventHandler statusHandler = StatusChanged;** if (statusHandler != null) { // Invoke the delegate statusHandler(null, e); } }
What does the bold line do? Why do we check for null? EDIT: The code is from C# Chat: Part 2 - Building the Chat Server (C# Programming Tutorial) • Geekpedia[^] check that out, if the above code doesn't provides what I mean.
Top Web Hosting Providers[^] Do, or do not. There is no 'try'.
-
// Holds the arguments for the StatusChanged event
public class StatusChangedEventArgs : EventArgs
{
// The argument we're interested in is a message describing the event private string EventMsg; // Property for retrieving and setting the event message public string EventMessage { get { return EventMsg; } set { EventMsg = value; } } // Constructor for setting the event message public StatusChangedEventArgs(string strEventMsg) { EventMsg = strEventMsg; }
}
// This delegate is needed to specify the parameters we're passing with our event
public delegate void StatusChangedEventHandler(object sender, StatusChangedEventArgs e);public static event StatusChangedEventHandler StatusChanged;
Okay, so in the above code, we created a delegate and a custom event.// This is called when we want to raise the StatusChanged event public static void OnStatusChanged(StatusChangedEventArgs e) { **StatusChangedEventHandler statusHandler = StatusChanged;** if (statusHandler != null) { // Invoke the delegate statusHandler(null, e); } }
What does the bold line do? Why do we check for null? EDIT: The code is from C# Chat: Part 2 - Building the Chat Server (C# Programming Tutorial) • Geekpedia[^] check that out, if the above code doesn't provides what I mean.
Top Web Hosting Providers[^] Do, or do not. There is no 'try'.
Hi, you make a local copy of StatusChanged, so the same value gets used both in the null-text and the invocation; StatusChanged is public, so it could be changed by another thread at any point in time; removing the last handler from it right after the null-test got executed would cause an exception in
statusHandler(null, e);
In summary, without it your code would not be thread-safe. :)Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
modified on Friday, June 10, 2011 12:01 PM
-
// Holds the arguments for the StatusChanged event
public class StatusChangedEventArgs : EventArgs
{
// The argument we're interested in is a message describing the event private string EventMsg; // Property for retrieving and setting the event message public string EventMessage { get { return EventMsg; } set { EventMsg = value; } } // Constructor for setting the event message public StatusChangedEventArgs(string strEventMsg) { EventMsg = strEventMsg; }
}
// This delegate is needed to specify the parameters we're passing with our event
public delegate void StatusChangedEventHandler(object sender, StatusChangedEventArgs e);public static event StatusChangedEventHandler StatusChanged;
Okay, so in the above code, we created a delegate and a custom event.// This is called when we want to raise the StatusChanged event public static void OnStatusChanged(StatusChangedEventArgs e) { **StatusChangedEventHandler statusHandler = StatusChanged;** if (statusHandler != null) { // Invoke the delegate statusHandler(null, e); } }
What does the bold line do? Why do we check for null? EDIT: The code is from C# Chat: Part 2 - Building the Chat Server (C# Programming Tutorial) • Geekpedia[^] check that out, if the above code doesn't provides what I mean.
Top Web Hosting Providers[^] Do, or do not. There is no 'try'.
Luc's answer is perfect. In addition, there's no need to declare your own delegate (from 2.0 onwards). Instead, just declare the event...
public event EventHandler<StatusChangedEventArgs> StatusChanged;
and in the OnStatusChanged...EventHandler<StatusChangedEventArgs> statusHandler = StatusChanged;
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) -
Hi, you make a local copy of StatusChanged, so the same value gets used both in the null-text and the invocation; StatusChanged is public, so it could be changed by another thread at any point in time; removing the last handler from it right after the null-test got executed would cause an exception in
statusHandler(null, e);
In summary, without it your code would not be thread-safe. :)Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
modified on Friday, June 10, 2011 12:01 PM
so the same value gets used both in the null-text and the invocation;
I'm sorry, but I didn't got it, what do you mean by null-text and the invocation? I'm converting this code to C++/CLI so I don't know C#, that's why having little problems... :)Top Web Hosting Providers[^] Do, or do not. There is no 'try'.
-
// Holds the arguments for the StatusChanged event
public class StatusChangedEventArgs : EventArgs
{
// The argument we're interested in is a message describing the event private string EventMsg; // Property for retrieving and setting the event message public string EventMessage { get { return EventMsg; } set { EventMsg = value; } } // Constructor for setting the event message public StatusChangedEventArgs(string strEventMsg) { EventMsg = strEventMsg; }
}
// This delegate is needed to specify the parameters we're passing with our event
public delegate void StatusChangedEventHandler(object sender, StatusChangedEventArgs e);public static event StatusChangedEventHandler StatusChanged;
Okay, so in the above code, we created a delegate and a custom event.// This is called when we want to raise the StatusChanged event public static void OnStatusChanged(StatusChangedEventArgs e) { **StatusChangedEventHandler statusHandler = StatusChanged;** if (statusHandler != null) { // Invoke the delegate statusHandler(null, e); } }
What does the bold line do? Why do we check for null? EDIT: The code is from C# Chat: Part 2 - Building the Chat Server (C# Programming Tutorial) • Geekpedia[^] check that out, if the above code doesn't provides what I mean.
Top Web Hosting Providers[^] Do, or do not. There is no 'try'.
Here is a very good article about events: C# Event Implementation Fundamentals, Best Practices and Conventions[^]
Giorgi Dalakishvili #region signature My Articles Asynchronous Registry Notification Using Strongly-typed WMI Classes in .NET [^] My blog #endregion
-
Hi, you make a local copy of StatusChanged, so the same value gets used both in the null-text and the invocation; StatusChanged is public, so it could be changed by another thread at any point in time; removing the last handler from it right after the null-test got executed would cause an exception in
statusHandler(null, e);
In summary, without it your code would not be thread-safe. :)Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
modified on Friday, June 10, 2011 12:01 PM
-
so the same value gets used both in the null-text and the invocation;
I'm sorry, but I didn't got it, what do you mean by null-text and the invocation? I'm converting this code to C++/CLI so I don't know C#, that's why having little problems... :)Top Web Hosting Providers[^] Do, or do not. There is no 'try'.
:confused: :confused:
if (statusHandler != null) <--- that looks like a null-test
// Invoke the delegate
statusHandler(null, e); <--- according to the comment this is an invocation:)
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
modified on Friday, June 10, 2011 12:02 PM
-
Luc's answer is perfect. In addition, there's no need to declare your own delegate (from 2.0 onwards). Instead, just declare the event...
public event EventHandler<StatusChangedEventArgs> StatusChanged;
and in the OnStatusChanged...EventHandler<StatusChangedEventArgs> statusHandler = StatusChanged;
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia):rose:
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
-
:confused: :confused:
if (statusHandler != null) <--- that looks like a null-test
// Invoke the delegate
statusHandler(null, e); <--- according to the comment this is an invocation:)
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
modified on Friday, June 10, 2011 12:02 PM
-
Isn't that just making a copy of the reference? And therefore no safer than testing/invoking directly on the original reference?
Subvert The Dominant Paradigm -- bumper sticker, circa 1971
Yes, it copies a reference, making sure the event and the delegates inside it can not be garbage collected (it is a local copy). There still is a possibility that the event gets changed (it is kind of a linked list), to cope with that you could (probably should) add a try-catch around the invocation. This is one of those rare cases where an empty catch block would be fine: after all, if another thread is allowed to remove the last delegate, that really means there is no need to call it anymore. Have a look at the fine article Giorgi provided. :)
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
modified on Friday, June 10, 2011 12:02 PM
-
Yes, it copies a reference, making sure the event and the delegates inside it can not be garbage collected (it is a local copy). There still is a possibility that the event gets changed (it is kind of a linked list), to cope with that you could (probably should) add a try-catch around the invocation. This is one of those rare cases where an empty catch block would be fine: after all, if another thread is allowed to remove the last delegate, that really means there is no need to call it anymore. Have a look at the fine article Giorgi provided. :)
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
modified on Friday, June 10, 2011 12:02 PM