 or let us see what the outcome of this video is. So generally the outcome after watching this video in this session would be to make sure the audience will be able to use thread and thread start classes in C-sharp. Of this, thread is a class while as thread start is a delegate. So as a prerequisite for this video, I expect that the audience is well-equipped with the fundamentals of C-sharp language wherein they know what exactly is a class and what exactly is a delegate. And we're going to use a C-sharp-develop ID and preferably here we are using 5.1.0 ID but a version doesn't matter as long as you're fine with using a proper .NET compatible .NET version and a compatible C-sharp-develop ID versions, okay? So let us move ahead. So let us see that live hands-on session and before we see a live hands-on session the prerequisite for you to replicate whatever I'm doing in this video. So let us move ahead and let me switch to a C-sharp-develop ID then we'll create a solution when we'll start writing our program, okay? So I'm switching to my C-sharp-develop ID and in this ID I already have a Hello World solution. So what we'll be doing is we'll create a new solution and we'll create a console application. So we are creating a threaded applications in a console mode and let us call this application as thread demo. So a solution which demonstrates how to create threads and how to invoke them. So I'll keep the location of project and the other names and other solution directors all default. Make sure that here you're very keen in observing that. I have selected C-sharp and under C-sharp the moment you select Windows applications and under Windows applications category you will find a console application. Make sure you're selecting the application type as console application, okay? So I have selected console application and I'm gonna click on create. This creates a solution for me, thread demo and at the same time you can see that here it has also added a code for me and this is a code from the standard template. So in order to make sure my screen is much well visible I'm gonna delete the upper comments which are auto-generated and now I'll increase the font here. To increase the font the shortcut is control plus plus, okay? Yes, so I think font looks a little bit legible now. So now we can write our code. So as of now I'll just remove them and I'll just retain console.readkey. Since it's a console application when we're running this application the program runs as a console application the moment output is printed it exits and it becomes quite tedious to debug that application. So we have put this line here where console.readkey just waits for user to enter a small key value. So we can check our output, the window won't exit here, okay? So let us create a thread. Now we know that a thread, see when we create a thread, thread's a lightweight. So basically when we create a thread thread is a part of process. It isn't allocated a new memory but it always shares the memory allocated for its parent process and as a developer when we are creating threads we should be more concerned about when I create a thread what the thread gonna execute in background. So as a developer I need to define that. For example in our application what we have decided is let us decide like what, let us decide a use case where what we are trying to do here is here the main task of mine is to print numbers from one to 20 and for me the order in which numbers are printed doesn't matter for the current use case which you are considering. So now what I'll do is I'll create a two threads one both the threads will be executing in background and one thread will print numbers from one to 10 and other will print numbers from 11 to 20. So here we have two threads one thread what it would do it would do a task of printing numbers from one to 10 and other thread would do the task of printing numbers from 11 to 20. So let us create that and in order to create a threads as I told you just as we have discussed that that every thread needs to have some action to be performed in background. So in our case these actions are nothing but a functions which will print numbers from one to 10 and 11 to 20. So here it is very important to note that while creating a threads in .NET here we just define a method whose signature would be always public void and we just need to create a thread delegate and make sure the thread delegate points to a method and these thread delegates will be invoked by a thread class. Okay, so let us do that. So we need to import few namespaces here for example the namespace for threading is system.threading. Okay, so let us import this namespace. So after importing that namespace system.threading now here in this program class we'll create two more methods. So keenly observe that the method signature. So what I'm going for is public return type is void and I'll say method one. So let us call this method one to 10. So this method will be used to print numbers from one to 10 and likewise we'll have one more method and the role of this method is to print numbers from 11 to 20. Now let us also write a small code. Let us write a for loop here. I equal to one from I'm we are starting from I less than or equal to 10 and I plus plus and here I'll write console.writeLine we'll print numbers line by line, okay? So here I'll just write I and the same code also goes into the pillar function as well just the values of I would change here. So you can observe that what are the code which I'm writing it is all identity. It's always very important that our code is always identity. So it helps us later to debug and it also improves my code readability as well. Okay, so these are two methods. So now I have two methods in the class program and in the main I need to write a code to so here goes the code, code for creating threads. So the first thing before we create thread is to create two delegates which will be pointing to the methods which I intend to execute in background with the help of threads. So let us create a two thread start references. Thread start we'll call it TS1. So I'm just creating this thread start references. Then I want this thread start reference to point to the method which I already wrote above. So here I have this method and since this method belongs to a class I cannot refer this method directly. So what I need to do is I need to create a program object. Since the methods belong to a class we cannot access them directly unless they are static. So now here I can point them. So obj. I can say method one to 10. So here it would be obj. method 11 to 20. Okay, so I have two delegates which are pointing to the method. So here is very important to note the method signature. So what method signatures do method needs to have if they are required to be pointed by thread start. They need to have a written type void and void. This is very important for that. Okay, remember, fine. So here now since I have thread start references I can create thread objects. So I'll call T1 is equal to new thread. So okay, so let me point out this. So we have multiple overloaded versions of constructor associated with thread class. I am more interested in using the first overloaded version this version which I'm talking about. So what it is accepting as a parameter to this it is accepting a reference to a thread start and to whichever method that thread start is pointing that method would be invoked when we invoke this thread here, okay. So I already have two thread start objects which are 3S1 and TS2. So I'll just provide here those references. Okay, so this gonna become my thread one and the next will be my thread T2. So I'm calling it a T2 and I want it to be TS2, okay. And since I am running my program on a very fast computer it's always easier for me to debug the output if I could put some thread.sleep so that I can see that output is printed in parallel. So we'll give a small say a 250 MS of delay here. See if you don't give a delay here what would happen is the programs and thread will execute at a such a speed where we won't be able to find out which thread is executing what it would be almost instantaneous you will just find an output. So generally for a thread programs we put a delay here but remember in production when we run our threaded application we don't put a delays. It's just for our purpose while we are learning that. Okay, so I have a threads created. So here threads are being created. So here I have, so thread objects. So now I'll invoke threads. So how do I invoke thread? Thread invocation, how do I invoke? So this is how I did it, T1 dot start that's a method which would invoke that thread. So now this has invoked thread T1 and this would invoke thread T2, the second call. Okay, so this is it. So now let us check this setup by compiling it. So what we'll do is we'll first try to like there are multiple ways. So what we can do is we can directly build solution. Build solution is equivalent to compiling that. So I'm building this solution and I'll check for output. Okay, so build finished successfully. So you can see here in the output window here build success means I don't have any errors here. So now I'll run this application and probably I'll run without a debugger. So it would open accounts. Okay, now you can see that. So it's printing numbers randomly here. So you can see that there is no order one and 12 and then I had 12. So we can see this. So this one was printed by thread one and this was a result of output from thread two. So you can see the randomness because here the issue is that I only have one console and two threads are trying to print. So the issue is that which would get access to console alternatively. So that's why we find a random output like one after other, one after other. So if you run this application once again the output would be somewhat different. Okay, so earlier it was one and then 1112. Now it is 1112. So let us be a little bit even more clear. So we'll just append some line here. So like thread one output. I'll call it is thread one and here I'll add as thread two. So let us build and see output now. Okay, build process can't, okay. Maybe that application is running. First exit this application. So now you can once again build. Check the output. Yes, build is successful. And now let me run it once again. I'm running it without debug. Okay, now you can see the thread two, one, two, two, two, thread one. Okay, this is how the threads are running in parallel. And you can see that why it is taking so much time to print them because we are given a delay here. Okay, so this is how we create a threads in .NET. So as a quick summary, what you need is you need to define a method which would be executed in a background by a thread. And once you define your background worker, what those background threads gonna do, then you need to properly create a thread start objects which would point to those methods. And with the help of thread class constructor, you need to provide the references of thread starts to those thread objects and then invoke those threads. So the call goes in this way, like three one dot start, you have started, you ask thread to execute. So what it would execute? It would just go to the thread start. And to whichever method that thread start is pointing, this method is executed in background by the thread. Fine, so given this understanding, so let us continue our further presentation here. So this was our presentation. So for you, I have a very quick question like, so if you have quickly followed my video and if you have keenly followed my video, you should be able to answer this question. The question is, can the thread start delegate point to a method with a signature whose written type is int and accepts null arguments, like void arguments, what do you think like? So you can like, if you have keenly followed the video, you should be able to recall like what is it allowed or not, the answer for this question is straightly yes or no. So you can pause the video and you can quickly recall or you can reverse the earlier part of this video and you can try to answer the question. But the answer for this is answer is no. Yes, so as I told you in earlier in this video, I told you that the measure signature has to be always written type void and input type of arguments as void arguments. So that's a standard enforcement. Rather than calling enforcement, the thread start delegates have been defined to point to methods of only that signatures, okay? This is with the help of the following method which I taught you. Now you should be able to write a multi-threaded program to implement matrix multiple. I'll know that the syntax and everything from Microsoft C shops, fundamentals, MSD and programming guide. And this is it. So thank you.