Previously we discussed about many basic fundamental topics such as Perfmon, JIT, different types of JIT, ngen exe, DLL, .exe, CLR, CLS, CAS, Methods, Datatypes, Casting and its types and also a little bit about Object Oriented Programming.
So now moving further towards our next discussion where we will learn about some topics having great importance in C#.
It sounds normal but Threading is a very important concept in C#. Let us take a simple example before understanding the exact definition. We all have Internet Explorer (IE) in our computer or laptop. Now when we open something in IE such as we search something and results come it shows everything on that page and then when we click on some link, that link directly opens in a new tab. And every time we try to open any link it always opens in new tab whereas the original tabdoes its work in background irrespective of the number of tabs that are opened. We call this process as Threading.
So now coming to the exact definition, we define threading as,
It is a concept or process which enables the C# to perform concurrent processing or simultaneous processing so that we can do more than one operations at a time.
A simple example to understanding this thing is that user puts some input values and then these values perform some of the tasks associated with the server in background. Now the user does not wait for the output, but instead he inputs the values and in background different task are happening. So simultaneously both the tasks are happening. In this way more than one task can be done simultaneously. Also Two or more program codes running simultaneously without affecting each other is called as MultiThreading, where each program code is called as Thread. With this example we understood the concept of threading.
In .NET Framework System.Threading namespace helps us to use threads easier.
Now associated with the threading we will discuss about Synchronous and Asynchronous.
Now let us first understand the general definition of synchronous. According to the definition synchronous means existing or occurring at the same time. Now how do we know that something is occurring at the same time? The answer is feedback, that is when we input something and we get the feedback of our input and after this feedback only we move towards next process or step. Due to this we come to know that the two processes are occurring at the same time.
For example, when we say that the speed of two cars are synchronized which means that when car 1 is driving at some desired speed, it is sending that desired speed information to car 2. Now car 2 receives that information and tries to match with the speed of car 1 and also sends the feedback to car 1 about matching the speed. Now as soon as car 1 receives that feedback it tries to maintain that speed so that it can be in sync with car 2. In this way by providing feedback to each other for every step until both the cars have their speeds matched. In short getting feedback at each and every step makes the process synchronous or we can say co-ordination of 2 processes with respect to time.
Now by this same principle the synchronous threading works. In this sync threading we create a thread or program code and execute it and wait for its feedback so that we can run another thread.
Now as the name it says, Asynchronous meaning is total opposite to that of Synchronous. Asynchronous means the processes are not occurring at the same time or are not in co-ordination with each other with respect to time.
In Async we do not wait for the feedback so that the next process can be started. It works in random manner.
In the same way Asynchronous Threading works, where we create and execute multiple threads at the same time but do not wait for its feedback.
Now moving further to our next topic of discussion which is also very important in programming point of view.
We have often heard this word when there are foreign talks going on between 2 countries or between several countries. So considering the general definition of delegates we come to know that it is a person authorized to represent others.
A simple example, when India is having foreign trade talks with other countries, we send an authorized person or delegate to represent our country in foreign trade talks. In the same way other countries also do the same.
So now coming to the technical definition for C#, Delegates are defined as pointers which points towards methods or functions. In other words we can create a pointer and point it to any method or function, after which we can invoke or call that method or function.
For example we can create a pointer which points to the Add method, after which whenever we want we can invoke that Add method via delegate or pointer.
How can we create a delegate?
- Declare a delegate.
- Create a reference.
- Point that reference to the method.
- Invoke the method via a delegate.
We understand this concept of delegate with a sample code given below:
public delegate int PointToDivision(int x, int y);
PointToDivision objpointer = null;
objpointer = Division;
Here first we are declaring a delegate and then we are creating a pointer which is pointing to the method Division. And then that division method is invoked via the pointer which was created earlier.
This is how delegates work in C# and .NET.
Now it happens that when we create a simple delegate it only points to one method or one function. But sometimes we want to point to multiple functions or methods and call or invoke them sequentially. This could happen if we use Multicast Delegate.
This is a higher level version of simple delegate. And the need for this is to point towards multiple functions or methods and invoke or call them in sequential manner. Therefore it is known as Multicast delegate.
To use this multicast delegate, we need to use “+=” sign. For example:
ptrcall += Add;
ptrcall += Subtract;
ptrcall += Save;
ptrcall += Delete;
Also Delegate is used in a 2 way manner because both the functions or methods or program codes can communicate with each through a delegate.
Now as we have already understood delegates the question arises that what are Events?
It can be defined as higher level of encapsulation of Delegates. Events follow in a 1 way manner because the main event or the pointer can point towards the functions or methods, but these functions or methods cannot communicate with the pointer or main event.
The Event follows the Publisher Subscriber mechanism model, where the different subscribers (functions) can subscribe to an event and the publisher can pass on the message to all these subscribers. The subscribers can subscribe or unsubscribe according to the choice but they cannot directly communicate with the publisher.
This process is also sometimes called as Broadcasting. We know the meaning of broadcasting where some message is broadcasted to all the members or subscribers, but these members cannot directly communicate with the broadcaster. Because Broadcasting is a 1 way mechanism.
A sample code will help us to understand the concept of Event.
public event CallEveryone MySpecialEvent;
obj.MySpecialEvent += Function1;
obj.MySpecialEvent += Function2;
Here we are first declaring the Event and then we are raising the event and then attaching or firing messages to different functions.
If we want to add subscribers then we put the sign “+=”and when we want to remove subscribers or unsubscribe an event we use the sign “-=”.
The delegate codes are naked or open and anyone can make changes in them, therefore to not allow any changes we cover up or encapsulate the delegate with an Event.
Now the question arises what is the difference between Events & Multicast Delegates? Both are pointing towards multiple functions or methods, so is there any difference?
The answer is yes there are some differences.
Difference between Multicast Delegate & Event:
- The first and the main difference is that Events follow Publisher Subscriber mechanism model whereas Multicast Delegates do not follow this model.
- Second is that Multicast Delegates can point towards multiple functions and invoke them in sequential manner. As it is one to one communication between delegates and functions, the functions also can communicate with the delegates.
- The event can point towards multiple functions but does not allow these functions to communicate with the event. Because here it is one to many communications between a single event and multiple functions. It works as broadcasting concept.
- The subscribers can anytime subscribe or unsubscribe to an event.