C# is an Object-oriented programming language. C# comes with simplicity, expressiveness and great performance to meet the programmer productivity.
Introduction
So far, we have discussed with
Events in
C#. Now, let's discuss about event accessors and using lock statements in C# programming in this chapter.
Objective
The main objective of this article is to learn event accessors and lock statements in
C# programming.
Event Accessors
Event accessors are the implementations of the Event's
+=
(add) and
-=
(remove) functions. We have already seen
how events works inside the compiler with the help of default event accessors.
Now let's see how to implement custom event accessors. Consider the event declaration below,
public delegate void Pen(); // Declaration of delegate
interface IPen // internal interface
{
public void InkStatus();
}
class Program : IPen
{
public event Pen MyPen; // Declaration of event
public event Pen IPen.InkStatus
{
add // add accessor
{
lock (MyPen)
{
MyPen += value;
}
}
remove // remove accessor
{
lock (MyPen)
{
MyPen -= value;
}
}
}
static void Main()
{
//..........
}
}
In the above example, we have declared a delegate
Pen()
and event
MyPen
from that delegate.
And also we have declared an internal interface IPen
.
add
and
remove
are the
contextual keywords of
C# which are used especially as event accessors. It is mandatory to specify
remove
accessor if you are going to specify custom
add
accessor.
If we do not specify any accessors, compiler automatically generates the explicit event accessors by default. That will be like,
private Pen _myPen; // private delegate
public event Pen MyPen
{
add { _myPen += value; }
remove { _myPen -= value; }
}
By this, delegate is updated via
lock-free algorithm.
If we define these accessors ourselves like in the above example,
C# compiler gets instructions that there is no need to generate the
field and accessor logic.
What is lock?
lock is the C# keyword which is used to mark a
statement block as a critical section. In most of the threading programs, locking is essential to restrict the code from being executed by more than one thread at a time. This mechanism increases the reliability of the program.
Example,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace Example
{
class Program
{
static readonly object obj = new object(); // readonly object
static void ObjMethod()
{
lock (obj) // locking the readonly object
{
Thread.Sleep(1000); // setting 1000 milli seconds sleep time
Console.WriteLine(Environment.TickCount);
}
}
static void Main()
{
for (int i = 0; i < 5; i++)
{
ThreadStart begin = new ThreadStart(ObjMethod); // creating thread
new Thread(begin).Start(); // Starting thread
}
}
}
}
In the above example, we have a
readonly object "
obj
". In the
ObjMethod()
, we are applying lock mechanism to restrict the code inside that from being executed by more that one thread.
In the Main() method, we are implementing a "for
" loop with 5 threads.
Now, press Ctrl + F5 to see the output something like below in your console,
In the above output, observe the difference of 1000 milliseconds for each thread (it may varies a little).
But the main thing is these threads are executed sequentially due to lock mechanism.
Conclusion
In this article, we have studied about event accessors and also usage of lock statements in
C# programming. Hope you understand.
Thanks for reading,
Regards
Krishna