How to multithread with C# 5.0 and .Net 4.5 - Part 1

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
well local youtubers today I want to talk to you about multi-threading parallel computing just a little intro to it and just so I don't waste your time I want to show you the final product to start with to see something that you're interested in spending some time learning how to do so this is going to be the final product and what I have here is an application that will be responsive but during a task and will also make use of all cores to process some data and then we have some cool things as the progress bar that will keep track of the progress and also some information on the writing tasks you can one way to queue data in here is just to keep clicking on the get some data here and then as you see I have some tasks that get queued up and at the same time the tasks are getting completed as well on the right side you have the task manager in here and you can see that the dotnet framework is actually making use of all eight cores that I have on my i seven they're not you know they're four cores and some of them are virtual I think I think that's it and then you have the progress you have the progress bar that keeps updating as I add new tasks so you can see that the progress bar is smart enough to know that new tasks came in so then the max gets you know goes down and the tests get completed the task bar gets closer and closer to to the end of it and that's what I want to show you how to accomplish today so if you find this interesting just stay tuned and just keep watching okay all right so you've made through the little intro I'm glad you stuck around I think we're actually going to have some fun with this application let me show you what I have so far and I have this already here just for the sake of time this is a windows form application I have of course a form and then I have a list box a progress bar a label and then a button inside the click event of that button I have just the three lines of code one is the query of their board the other one is calling this function called get data and then when the data is returned from this function I insert that data into that list box at the very top okay the get data function really doesn't matter in here what I'm trying to simulate is some long-running task it could be a call to a database you could be a call to a web service somebody that takes some time to respond I'll show you what I have and all I'm doing here is creating some variables and then looping over and over and over and the reason why I use a loop instead of putting the thread sleep is because I actually want the CPU to do some work that's why I'm the demo you saw the CPU getting used all going to the very top I could have just simply put the CPU bet threads asleep but then I wouldn't you wouldn't see the CPU being used so this is just again just to simulate a long-running task and the application right now clearly is not threaded at this point so let me show you what the application looks like at this point the purpose bar is not implemented even though it's present here nothing's going to happen to the progress bar and the running tasks this label is not going to get updated at this point so when they click get some data the button one click it gets called and then distri lines of code get executed the problem with this is that when I click it and I try to move the form around you can see that he didn't move until that that and that task got completed so and also if I click several times when we click three times real quick one two three you can see that I can't move and I'm not getting updates as I'm not giving the user information about what's happening with the application the application at this point kind of just freezes for the user it's very busy in the back but for the user just freezes and then boom dumps all the data back and so we're going to try to fix this in front we're going to fix this this problem at this point by using tasks and what tasks are are you can think of tasks as units off work and what a test does it actually Forks the application into a separate thread so then the UI is free to provide information to the user as the second thread is busy second or third or fourth a thread is busy doing work that they use requesting so let's go ahead and introduce tasks it's a simple list using the task library let me go ahead and import the the namespace and I'm going to create a new task T says new task and then right now I have to use something called a lambda expression and Alembic expression is a limb expression is it's a nameless or it's like a nameless parameter no a nameless a function so imagine so even if you know how I have to get dating here and it takes it takes no it takes no parameters that will be the lend expression that we're going to create so instead of doing like some fun method right and then you can put the body of the method in here like this right that would be that would be a method right I'm actually that's actually pretty good a good way to look at it so this is a method alright some method takes no parameters and then you have the body of the method the limit expression is just a little bit different you don't put a name to it you put the parentheses and then any air instead of putting the brackets you're going to put it like goes to and then we're going to close with the parenthesis and a semicolon that's our task right there I'm but you still need another line of code here so we declared the task but the system is not going the system is not going to execute that task I don't at the same time you actually have to tell it to actually start so you have to call it teeth I start and then that would actually start this it will start this three lines of code in a separate in a separate thread forked from the main thread now there is a problem here and I'm going to show you what that is so I'm gonna go ahead and click start and you see that we're gonna get an exception here so I'm gonna click get some data I can actually move the UI around at that point did you see I could actually move it around as he was trying to accomplish because he actually fork that ain't you a separate thread but the problem here is that that second thread that was executing the calculation try to update the list box with you know we tried to insert that data into the list box but that list box in this is in a separate thread is on the UI thread and so we got this a valid operation exception it says it's called a cross thread operation on valid you cannot you cannot access data across threads you have to merge it back so let's go ahead and see how we do that in order to be able to update update objects that are on the UI thread we actually have to separate this a little bit and one way to do this is I'm going to move this string data here to let me go ahead and stock application I'm gonna have I'm gonna move this string data outside that task and I'm also going to move this outside the task okay go ahead and do this and then what I'm going to do here is create a second task okay I'm going to call this t2 but I'm not going to new up on new tasks I'm not going to say I'm not gonna start it Koopa Lily no separate tasks I'm going to say that this task number two is going to be a continuation off the first task okay continue with the first task and then I'm also going to use a lemon expression but this time does lend expression will actually take a parameter okay so then the parameter the crab hang here will be you can name your parameter whatever you want but I'm just going to call previous tasks okay and then I'm gonna do goes to and then the body off the task again okay just like we did it on the first one the difference here is that now I have a second parameter to discontinue with function okay hope you're following me here so this is a function the first parameter is this whole thing here okay this is the first parameter and then the second parameter is this idea some types take so I'm going to put a comment here I'm gonna call the task scheduler that from current synchronization context and now I'm going to close this there you go it's suggesting that I use limit expression I'm not just I'm just gonna leave it like this okay so what happened here what happened here is that now I have two tasks and then this initial task is Forks the program into a second thread does the processing and then it merges back into the second thread and the second thread is running on the current secret it's on the current context which is the UI context that's one way to get back into the UI context and so now we have data to update the UI thread and then we call the start task so if we click start here and we do get some data you can see that I can't move it around okay and as I move it around data actually showed up on the list box okay so now our application is already much more much more responsive I'm going to click like three times one two three and then as I move the application around you can see that tasks are being processed and executed let me click a few more times and as I'm clicking things are going to get processed and I can keep adding more to it okay s s data's be processed at the same time ok as I'm adding tasks the tasks are in process and then I can move it around already so our application is much more responsive already and not only not only does the task create new new threads but since I have a multi-core system the system is Mart enough to actually assign those threads into multiple into different cores so now I have the application running in parallel ok so not only is multi-threaded but is actually parallel as you can see it's using multiple cores to process all that information this is this is it to create this is it to create a multi-threaded application and be able to pass information back to the system there's a lot more to the task I mean you can actually use tasks that return data instead of using a variable like that but let me also go ahead and show you how to update the progress bar and this is actually and then you can give more you can give more information to the user
Info
Channel: Programming with Fabio
Views: 15,293
Rating: 4.9619045 out of 5
Keywords: c# multithreading, multithreading c#, multithreading in c#, c# multithreading example, c# multithreading tutorial
Id: 31tHiGD8X-A
Channel Id: undefined
Length: 12min 23sec (743 seconds)
Published: Sun Dec 01 2013
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.