C# Threads, Tasks, Multi-threading & UI Cross-threading

  Рет қаралды 97,226

AngelSix

AngelSix

6 жыл бұрын

In order to understand more complicated code that includes threads, Tasks, awaits, async and more, we first need to understand the underlying system and concepts that power them all... Threads.
In this video we take a look at what a thread is, what it means, how to create one and many, what effects threads have on our code in terms of speed, structure and flow.
We then take a look at the thread pool, tasks and what happens when you try to edit UI components from a non-UI thread and how to fix that.
Support Me: / angelsix

Пікірлер: 132
@jessegador
@jessegador 5 жыл бұрын
Years of wanting to understand thread until this excellent video.
@AngelSix
@AngelSix 5 жыл бұрын
Glad it was useful!
@rishitsingh6621
@rishitsingh6621 3 жыл бұрын
Same
@mrsajjad30
@mrsajjad30 3 жыл бұрын
Thank you so much Sir! For me this channel is the first stop for learning any thing about C# and dot net so it was a big relief when I wanted to learn about multithreading and found the topic here.
@parthasarathipatra9483
@parthasarathipatra9483 2 жыл бұрын
I was struggling a lot with these concepts until I watched this. Massive thanks to you for your time and effort.
@guyrapong
@guyrapong 2 жыл бұрын
it's literally what I needed to learn for a project of mine, wait for the download to be over so it can be unzipped automatically, then wait (again) to continue. amazing content. thank you :)
@adamfarmer7665
@adamfarmer7665 3 жыл бұрын
I have used threads before, but I never understood it "this clearly". You also give some intuition on how things work. Very valuable.
@kopilkaiser8991
@kopilkaiser8991 Жыл бұрын
Damn. You are such a great and spectacular teacher. I highly recommend any newbie on this topic to watch this video. Since, a lot of the other videos over complicate things whereas you have simplified it and made it possible to understand. On the contrary, the Tasks is a vast topic and one requires practice to be done simultaneously by themselves to learn this more clearer and the logic will be crisper in the head.
@mck9512
@mck9512 4 ай бұрын
Good job on the strategy how to explain this topic. It worked. Been working for over a year on a wpf project, barely understood what i was doing with threats, tasks, etc. Now, its much more clear.
@simplyincorrigible7708
@simplyincorrigible7708 5 жыл бұрын
Oh my, this is a nice rabbit hole. Great vid. I just used this multi threading to calculate primes 2 to 100 million in 2 seconds.
@NewLondonMarshall
@NewLondonMarshall 3 жыл бұрын
It definitely increases performance!! I will use it to calculate factorials faster!
@MrBomberman11
@MrBomberman11 4 жыл бұрын
Wow I have literally been looking for exactly this type of video for days. Great video mate.
@AngelSix
@AngelSix 4 жыл бұрын
Glad you found it :)
@Aryevang
@Aryevang 2 жыл бұрын
I keep coming back to this video to remember and the ways to use Threads and task in C#.
@MrHonest10000
@MrHonest10000 5 жыл бұрын
Fantastic explanations and demonstrations for understanding threads. Thank you.
@AngelSix
@AngelSix 5 жыл бұрын
Your welcome. Glad I could help
@kopilkaiser8991
@kopilkaiser8991 Жыл бұрын
Liked, Subscribed, Added your video to my playlist. You have earned my love and respect. You are an excellent tutor. Keep it up.
@NikolayRogchev
@NikolayRogchev 5 жыл бұрын
This video should have a lot more likes... You obviously have deep understanding of the topics and you can explain it well enough, considering it is quite abstract topic. Good job, I am subscribing to your channel
@AngelSix
@AngelSix 5 жыл бұрын
Thanks, glad I could help
@MeldsaGames
@MeldsaGames 6 жыл бұрын
Great video as always Luke, keep up the good work!
@AngelSix
@AngelSix 6 жыл бұрын
Thanks and will do
@hollisross3561
@hollisross3561 3 жыл бұрын
Thanks for the guide on threads. It was very helpful.
@rodionradchenko7708
@rodionradchenko7708 6 жыл бұрын
🔥🔥🔥 I've been waiting for this! Thanks, Luke.
@AngelSix
@AngelSix 6 жыл бұрын
:+1
@douuuglas
@douuuglas Жыл бұрын
Awesome content and quite well explained!
@j0nj1
@j0nj1 4 жыл бұрын
Your videos are great. Thanks for all your hard work!
@AngelSix
@AngelSix 4 жыл бұрын
Your welcome
@doughaskins3995
@doughaskins3995 2 жыл бұрын
Great stuff. Looking for more now. Thank you. :)
@sallashwin
@sallashwin 5 жыл бұрын
Brilliant! Very well done.
@tomas120
@tomas120 6 жыл бұрын
I have been waiting this for a long! Thanks you!!
@AngelSix
@AngelSix 6 жыл бұрын
Your welcome
@arshajacob6772
@arshajacob6772 5 жыл бұрын
You are out of this world...Too good
@AngelSix
@AngelSix 5 жыл бұрын
Thanks. Glad I could help
@robertoteixeira9122
@robertoteixeira9122 6 жыл бұрын
Extremely useful as always. Thanks!
@AngelSix
@AngelSix 6 жыл бұрын
Your welcome
@Mrusmanjjjj
@Mrusmanjjjj 6 жыл бұрын
Thanks for another great tutorial
@AngelSix
@AngelSix 6 жыл бұрын
Your welcome
@Johnn84
@Johnn84 5 жыл бұрын
Thanks for making this video.
@ryantaylor3393
@ryantaylor3393 6 жыл бұрын
Great Tutorial!! Very Helpful!!
@AngelSix
@AngelSix 6 жыл бұрын
Thanks
@vinuhosanagar1
@vinuhosanagar1 6 жыл бұрын
Thank you for all your work :) :)
@AngelSix
@AngelSix 6 жыл бұрын
Your welcome :)
@quantume7143
@quantume7143 2 жыл бұрын
Great video! Thank you :)
@aramnasser268
@aramnasser268 5 жыл бұрын
Awesome, thanks a lot. Keep up the good work :)
@AngelSix
@AngelSix 5 жыл бұрын
Thanks, will do
@Sunroars
@Sunroars 5 жыл бұрын
Very Helpful..!!
@ANILGOINDI
@ANILGOINDI 4 ай бұрын
You are a gem!
@vidyasagarpamula794
@vidyasagarpamula794 6 жыл бұрын
Thanks. You made my day 😊
@AngelSix
@AngelSix 6 жыл бұрын
Glad I could help
@lukeaustin3313
@lukeaustin3313 3 жыл бұрын
Very useful thank you!
@KaushikVapiwala
@KaushikVapiwala 3 жыл бұрын
Great Video!!!
@everyonesview
@everyonesview 3 жыл бұрын
Awesome! Cheers mate.
@indsaran
@indsaran 2 жыл бұрын
Absolutely Super. 👏
@mohieadeenaljudra1388
@mohieadeenaljudra1388 6 жыл бұрын
Great tutorial thank you so much
@AngelSix
@AngelSix 6 жыл бұрын
Your welcome
@inRable
@inRable 6 жыл бұрын
awesome vid!
@AngelSix
@AngelSix 6 жыл бұрын
Thanks
@donfeto7636
@donfeto7636 5 жыл бұрын
bro could you tell me the source of your information you are so awesome in teaching does i need to read books or what to be like you , you are my Idol
@phy5prt
@phy5prt 4 жыл бұрын
Really useful thanks
@AngelSix
@AngelSix 4 жыл бұрын
Your welcome
@ctbram0627
@ctbram0627 5 жыл бұрын
UI Cross-Threading is EXTREMELY important because the first thing you run into in the real world is trying to update a component in the main UI thread from a different thread and then you start googling when you start getting the crash messages and then find there are dozens of different ways developers handle this and your brains are all scrambled!
@AngelSix
@AngelSix 5 жыл бұрын
It's pretty simple and very easy to understand but too many people and stack overflow and the internet in general f*cked it up and confused people.
@doughaskins3995
@doughaskins3995 2 жыл бұрын
@@AngelSix I agree. I have been trying to learn this and OMG, so much misdirection and sample code that doesn't work. It is incredible. Your video is simple and to the point. Thank you.
@sabitkondakc9147
@sabitkondakc9147 3 жыл бұрын
Hi Luke, your explanation is rather neat, clean. In years to come ,do you have a plan to create materials on Rust Programming Language which a new technique to deal with Memory Leak , Efficient Memory Management? In the meanwhile , will you be on Udemy next years?
@jordanguada4354
@jordanguada4354 3 жыл бұрын
great vid thanx
@easterdays4208
@easterdays4208 6 жыл бұрын
thank you so much! :)
@AngelSix
@AngelSix 6 жыл бұрын
Your welcome
@phil7extreme
@phil7extreme 4 жыл бұрын
At 53:27 what would happen if you change the content of MyButton outside of Task.Run(() => {...}); So if you would write MyButton.Content = "Logged in"; on line 45? Wouldn't that then be executed by the UI Thread aswell?
@silentgut
@silentgut 4 жыл бұрын
What can i do if i want to do a multi thread using DI where in my class will be instantiate differently?
@shahrukhkhan3967
@shahrukhkhan3967 4 жыл бұрын
Can you increase the font size of Visual Studio ? I can't clearly see the code
@AngelSix
@AngelSix 4 жыл бұрын
I do that in future videos
@russelldodd6087
@russelldodd6087 4 жыл бұрын
Giving me migraine squinting at this code. I hope the uploader will redo the entire video in HD.
@DMC428
@DMC428 4 жыл бұрын
25 People doesn't speak english... Great video... Have to watch your Task-Video now :)
@paulus7735
@paulus7735 3 жыл бұрын
"people" is a plural noun, so they "don´t" ; Actually his dialect is terrible, I couldn´t stand it longer than 10 min.
@DMC428
@DMC428 3 жыл бұрын
@@paulus7735 thx for the education! English is my third language but I try to keep that in mind.
@shahrukhkhan3967
@shahrukhkhan3967 4 жыл бұрын
You are saviour (Y)
@AngelSix
@AngelSix 4 жыл бұрын
Glad I could help
@Paragparashar03
@Paragparashar03 2 жыл бұрын
Nice lecture!!!!! If you could increase the font size in future videos, that would be really helpful!!!!
@djelasrca8091
@djelasrca8091 4 ай бұрын
What if you want to have element that is constantly moving on your UI. Is it possible to render it on a different thread?
@patrikbak8161
@patrikbak8161 6 жыл бұрын
Very good explanation. Question: What would be different if you removed await Task.Run(async () => {...}) and just used await new HttpClient().GetStringAsync(...)?
@AngelSix
@AngelSix 6 жыл бұрын
The only difference is everything up upti the await new HttpClient... part would be on the original thread, then this single call would jump to another thread, and after that back to the original thread.
@yogibobo1
@yogibobo1 5 жыл бұрын
Great video, Do you have any exercises or can recommend on such place which have so I can practice it?
@AngelSix
@AngelSix 5 жыл бұрын
Good question. I don't at the minute but I could think of some challenges maybe
@yogibobo1
@yogibobo1 5 жыл бұрын
@@AngelSix bring it on It will be wise if u make a few levels of diffculties U R great mate
@chamir4614
@chamir4614 6 жыл бұрын
Hi!, how will you make the message system of sending and retrieving? Using SignalR or something like that?
@AngelSix
@AngelSix 6 жыл бұрын
It will be many things that make the entire product. The front-end website will use SignalR and javascript AJAX in a combination, the client apps will use signal R and push notifications and the desktop apps signal R and whatever push system each specific OS uses
@chamir4614
@chamir4614 6 жыл бұрын
Will you do a video explaining all that?
@AV1461
@AV1461 4 жыл бұрын
1:02:27 So, I still didn't get the idea of using async/wait. If you're going to hand-over the task to another thread and then wait for it, how isn't that blocking? Or is other stuff outside of MyButton_click() function good to execute somehow?
@AngelSix
@AngelSix 4 жыл бұрын
Watch the video on Tasks it explains it
@AV1461
@AV1461 4 жыл бұрын
@@AngelSix Ah yes, you mentioned. I will.
@everyonesview
@everyonesview 6 жыл бұрын
Thank you seems so small when YOU are doing so much more. I was wondering if could you possibly change your VS Color Theme to anything other than the Dark in your next video. Please? I think, and I hope many people would agree with me, that the other Themes enhance legibility more. I'm very grateful to you, Luke.
@AngelSix
@AngelSix 6 жыл бұрын
i started videos with light theme and many many people complained due to it being so bright. Then once I started down the "dark theme" route I turn everything dark now to save my eyes. I could change the color scheme perhaps but the majority of people including myself prefer it dark
@jakealexandermurray9977
@jakealexandermurray9977 6 жыл бұрын
Holy shit this is helpful!
@AngelSix
@AngelSix 6 жыл бұрын
Cool, I'm glad it was :)
@ihy1234
@ihy1234 4 жыл бұрын
39:20
@gamehubby
@gamehubby 6 жыл бұрын
Awesome, how big your screen monitor?
@AngelSix
@AngelSix 6 жыл бұрын
Been using philips 43" for a year now. Just changed down to a 32" to fit more recording equipment on my desk
@1nfility614
@1nfility614 6 жыл бұрын
hi, do u know then how u can create tasks in your UI dynamicly by clicking buttons? :-)
@AngelSix
@AngelSix 6 жыл бұрын
I don't get the question. It's too vague. Can you give more detail?
@1nfility614
@1nfility614 6 жыл бұрын
basicly, in sneaker bots, u can create tasks, and then when u click one button, all these tasks, send requests, so if its 3 tasks in the UI. with same settings. its the same requests and everything * 3 [here is a little snippet of what i am referencing to = kzfaq.info/get/bejne/n7tzl9ty15jPhGw.html ]
@AngelSix
@AngelSix 6 жыл бұрын
Yeah it can be done just coding it yourself in C# easily, but its a bit much to write the code in just a comment, and I don't really have time at the moment to write it out. If your join my discord I am sure someone on there will help though discordapp.com/invite/eHr5BMk
@chancesleon6517
@chancesleon6517 5 жыл бұрын
Threads depends on CPU, right? like how much it can go slow/fast..?
@AngelSix
@AngelSix 5 жыл бұрын
It's a fairly complicated answer. The CPU cores deal with threads, and they can have many virtual threads and physical threads, but the work threads do can depend on anything (RAM, HDD, Ethernet, CPU, GPU etc...). The basics of creating threads and providing execution time is CPU bound though
@mgeorgescu
@mgeorgescu 5 жыл бұрын
A single core CPU will always run a single process/thread at a given time. It switches the context ( switches between threads/process) very fast to give the illusion of parallelism, it schedules CPU time for each task. In a multicore cpu, you will have real parallelism, two threads will run in two different cpus, but each cpu can process multiple threads as in the case we explained before.Think of a thread as a chain of cpu instructions. More or less this is how it works.
@vharish9114
@vharish9114 6 жыл бұрын
Hi ,Iam fresher. To become good web app developer, I should know windows app development also?
@AngelSix
@AngelSix 6 жыл бұрын
v harish there is no need but knowing both makes you a more well rounded developer
@MultiMinors
@MultiMinors 6 жыл бұрын
Its sad that you didnt show any examples in MVVM environment :( You also forget to say something about working on multi-threading in class library (like .Core in fasetto), as we can't call Application.Current.Dispatcher here I hope for your next video on tasks that you will explain these things
@AngelSix
@AngelSix 6 жыл бұрын
This is in the general programming videos nothing to do with MVVM and doesn't have any implications with that. For using it in Core I will cover how to do that using IoC/DI in another video.
@killermaker7218
@killermaker7218 3 жыл бұрын
Nice video, but next time be sure of making the font bigger pls, i could barely see what u were typing
@jarno2427
@jarno2427 3 жыл бұрын
Great vidoe my program is like 20% to 25% faster now
@SoferPeOZN
@SoferPeOZN 4 жыл бұрын
39:20 lol
@sweatyhands9830
@sweatyhands9830 4 жыл бұрын
puggo is somewhere in the video
@samirjan9763
@samirjan9763 4 жыл бұрын
next time when you try to record video. google "how to zoom visual studio". then we can see what you write
@AngelSix
@AngelSix 4 жыл бұрын
I do in future videos make it larger
@kritikyorumer
@kritikyorumer 2 жыл бұрын
i watched
@manofqwerty
@manofqwerty 3 жыл бұрын
The main reason the 1000 threads took so long is just writing to the console.
@LordErnie
@LordErnie 3 жыл бұрын
The download is already a task. Why wrap a task inside of a task? For this to be proper you would just await the download without putting the download that is already a task inside another task. You WOULD however do this if your download isn't an async call.
@katerose6161
@katerose6161 3 жыл бұрын
Font is a way too small. My monitor is 23'8" and play quality is 1080p. But this video is impossible to be watched. :(
@darkydrone
@darkydrone 2 жыл бұрын
Holy fck... between 30 and 33 min was painful to watch. Other than that, it's a nice explanation.
@darkdragon6449
@darkdragon6449 5 жыл бұрын
Nice work.. Bt please can you speak more clearly and slowly :)
@sankas1998
@sankas1998 5 жыл бұрын
Go Settings->Speed
@samueldebeer2306
@samueldebeer2306 2 жыл бұрын
I know I'm 4 years late, but there really is no point in using Task.Run() here. We don't need to use another thread from the thread pool to simply await the GetStringAsync(). All you need to do is await (new HttpClient().GetStringAsync()) from the UI thread. This will result in the UI thread starting the asynchronous GetStringAsync task and it will be free to take in any interface inputs while the task is running. Once the GetStringAsync task is done, the rest of the code in MyButton_Click (which is now a continuation) will be executed by the UI thread. The information in this video was useful up until this point, but the async/await section at the end will give many people the wrong idea.
@AngelSix
@AngelSix 2 жыл бұрын
I think you didn't watch it fully at the end. It isn't to say that is how you do a call to that specific method. The point of the end is to show you what ConfigureAwait() does if you are using a Task.Run
@mrgarconjimmenald3821
@mrgarconjimmenald3821 4 жыл бұрын
My only complaint is after living in a cube farm for 20 odd years, I can't stand to listen to the click and clack of keyboards. It would be much better if you filtered or setup the mike in some way to lessen that.
@AngelSix
@AngelSix 4 жыл бұрын
I've moved to cherry reds now the quietest mechanicals
@KhalidAfridi1
@KhalidAfridi1 4 жыл бұрын
Unnecessarily long and boring video, also difficult to see the code
C# Tasks Async Await
2:34:08
AngelSix
Рет қаралды 103 М.
Spot The Fake Animal For $10,000
00:40
MrBeast
Рет қаралды 195 МЛН
Пранк пошел не по плану…🥲
00:59
Саша Квашеная
Рет қаралды 7 МЛН
8 await async mistakes that you SHOULD avoid in .NET
21:13
Nick Chapsas
Рет қаралды 310 М.
Master Multithreading : Thread Pool In C++
20:19
CppNuts
Рет қаралды 8 М.
C# Async Await Mistakes | Part 1
10:19
Amichai Mantinband
Рет қаралды 32 М.
What are ASYNC and AWAIT in C#? Asynchronous Programming Tutorial
18:34
Writing async/await from scratch in C# with Stephen Toub
1:06:02
C# Events - Creating and Consuming Events in Your Application
1:09:13
Advanced C# - LINQ Tutorial
3:07:24
freeCodeCamp.org
Рет қаралды 115 М.
Stop using async void in C#! Do this instead.
13:12
Nick Chapsas
Рет қаралды 57 М.
Проверил, как вам?
0:58
Коннор
Рет қаралды 378 М.