C# multithreading 🧶

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's going on everybody it's bro hope you're doing well and in this video i'm going to explain multi-threading in c-sharp so sit back relax and well enjoy the show if you find this video helpful please remember to like comment and subscribe your support will help keep this channel running all right all right all right threads a thread is an execution path of a program we can use multiple threads to perform different tasks of our program at the same time so when we begin a program we have one thread that is running and that is named the main thread to work with threading include at the top using system dot threading so let's get the current main thread that is running and i'll assign this to a local variable of the thread data type let's say thread main thread equals thread dot current thread so this will assign the current thread that is running to a local variable named main thread or whatever you want to name it so i'm going to change the name property of main thread to equal let's say main thread and then i'll display this with a right line statement main thread dot name and the name of this thread is main thread here's our job let's say that we need two timers to run one is counting up from zero to ten and the other is counting from ten to zero one is counting up the other is counting down but we need both of these timers to run concurrently so if we were to write these both on the same thread this is what this would look like and then later on we'll have these timers run on different threads so let's create a countdown and count up method public static void count down and then i'll need a for loop to iterate 10 times 4 and i equals 10 then i will continue this as long as i is greater than or equal to 0 and then decrement i by one during each iteration and during each iteration let's say that timer number one colon space plus i plus seconds now we can actually have our current thread that is running sleep for a given amount of milliseconds by typing thread dot sleep then pass in how many milliseconds you would like your thread to sleep so 1000 milliseconds for one second and then when we escape our for loop let's display that timer number one is complete and we'll also need a count up method so let's copy countdown paste it change down to up i is equal to zero we will continue this as long as i is less than or equal to ten increment i by one during each iteration timer number two timer number two is complete so let's invoke both countdown and count up so remember that these are both running on the same thread our main thread and when we complete our main thread let's display that main thread dot name plus is complete and i'm just going to hide this line here okay let's go so we have timer number one that's running currently timer number two has not started yet because these are both running on the same thread so timer number one is complete and now we're working on timer number two and now timer number two is complete our main thread is complete so if i would like both of these methods to execute at the same time i would need to create some additional threads and here's how to do so thread let's name this thread 1 equals new thread and within the constructor of our thread let's pass in the method we would like to execute thread 1 is in charge of counting down and thread two is in charge of counting up so each of these threads has a built-in start method which we need to invoke to begin each of these threads thread1.start and thread2.start and we can get rid of these we now have one thread in charge of counting down and the other thread is in charge of counting up and our main thread is still running in the background so our main thread is complete and we have one timer that's counting down and the other that is counting up and as you can see these are both running at the same time concurrently so if you have a method that has parameters there's one additional step let's say that we have i don't know a string name or something we don't necessarily need to use these okay so we can't use these as they are currently so what we're going to do is within the constructor of our threads is pass in what is known as a lambda expression so parentheses arrow then the name of our method countdown parentheses than any arguments if there are any let's say i don't know timer number one i probably won't use this at all but this is just for an example okay let's do the same thing with thread two so we have a lambda expression parenthesis arrow and this is in charge of counting up and passing i don't know timer number two i guess i might have to make a separate video on lambda expressions okay let's try this again so this should work all right there we go so yeah those are threads a thread is an execution path of a program we can use multiple threads to perform different tasks of our program at the same time when we begin a program we have one thread that is running and that is referred to as the main thread so yeah that's multi-threading in c sharp if you would like a copy of this code i will post this to the comment section down below and well yeah that's multithreading in c sharp hey you yeah i'm talking to you if you learned something new then help me help you in three easy steps by smashing that like button drop a comment down below and subscribe if you'd like to become a fellow bro [Music] you
Info
Channel: Bro Code
Views: 59,063
Rating: undefined out of 5
Keywords: c#, multithreading, threads, c#, tutorial, c#.net, .net, dot net, dotnet, multi threading, multi-threading, example, concepts, multithreaded programming, what is process in c#, c# what is thread, c# create a new thread, difference between thread and process in c#, c# thread start method, c# thread class example, c# thread sleep example, c# thread execute method, c# process vs thread]
Id: rUbmW4qAh8w
Channel Id: undefined
Length: 6min 58sec (418 seconds)
Published: Sat Jul 03 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.