From the course: C#: Advanced Practices

Distinguish asynchronous and multithreading - C# Tutorial

From the course: C#: Advanced Practices

Distinguish asynchronous and multithreading

- In our last video, we spoke about handling asynchronous errors. In this video, we're going to talk about asynchronous code and threads. So Bill, I know that .net framework is a multi-threaded and I'm pretty familiar with JavaScript which uses async single threads, but is asynchronous multi-threaded or is it single threaded? - So in this instance asynchronous can be both or either. Just like promises, you can do similar kinds of contracts which will be on a single thread or because .net can do multi-threaded programming. You can do things on multiple threads. The great news is that unless you're really trying to share data between multiple threads and process in that way, you usually don't need to worry about it. So you can think of it similar to the model that you would use with JavaScript promises. - Nice. And, but how can I tell if I'm using a single threaded or multi-threaded? - Well, let's actually run through an application and we'll really get to that point where it doesn't matter so much. So this is an app that we wrote and it's from one of our tutorials. And what did it does, is this going to make several requests to GitHub and print out some information about the most recent issues that we got on docs. And a couple of things I like is if you notice it kind of spit out a bunch of things and then pause for a bit and then spit out some more issues and pause for a bit. When we grab issues from GitHub, we'll grab like a page worth about 25 and then they're all here and I could write all those to the console and then we have to go make another request and get the next 25. - Does this use multiple threads? - Well, let's see. So I'm going to start here at the beginning of the main application, and I'm going to write just exactly what thread ID we're working on. So right there, I've got this Console.WriteLine Entering Main: and this Thread.CurrentThread.ManageThreadId that's what thread we're on. And then as I enumerate each of these issues which is something we learned about in link, I'm going to write another one and I'll just here, we're going to say, I'm writing issue. And then down here in the asynchronous code, where we go in and get each page you can see I'm going to be grabbing pages. And right here, I'm actually making a Post request up to GitHubs graphqlapi. So before I make that, we'll add one here and we're going to say, Requesting Data and then I'll add one right after that data comes back after we've awaited. So that's one of those things where it went asynchronous and we'll say we Got the data. And then down here below we're using one of our newer features which is async renewables, which is a new thing as C-sharp eight. And as I yield return, each one of these issues I'm going to say, Returning issue is this is an interesting piece of code and that it looks like it could be async, but there was that chunking that we saw where once I got a full page it's going to synchronously return each one and then when it got to the last one on a page, it's going to have to go back and request the next page and there it's actually going to be asynchronous. So now after adding these right lines let's run it again and we'll get hopefully the same issues in case people are really angry with it. But you see, we're starting to get some of these things where now we're seeing different thread IDs - Right. - And it's mostly thread seven. And there's a few really interesting things here. Let's go back to where I started. - Yeah, I see thread seven. And if you scroll up, Yeah, so it mostly is using thread seven - And the very beginning. - Oh, I think I see. - You see some fours. - Yeah, I think I just saw a thread four. - Right and they're at the very top. It started on thread one, which I saw as it rolled by in the very first part. And then about halfway through, we switched over to the thread ID four, but mostly we're on seven. So what's happening here is that we have a thread pool and in the console application, all the threads are pretty much the same. They're in the same context working on the thread ball. So any thread is available and any thread is pretty much the same as any other one. So it will just keep using this same one or grab whichever one might be available. And occasionally they switch sometimes it doesn't and that's okay. - Right, and so is that why this application sometimes uses different threads? - Right, so after an await it's going to pick whatever threads available from the same context because they're all in the thread pool, they're all the same and then continue on that. Whereas like, if this were a WPF program like in our last application, there's only one thread that can write to the UI. So in that instance, the continuation runs on exactly the same thread to make sure that it can update the windows correctly. So it will stay on just one thread. Okay? - Yeah. Nice. So all this async code pretty much allows me to scale my app really well. - Right, because the language just does the things to grab whatever thread is available, and that's one of the ways where asp .net core scales extremely well is all this asynchronous code means anything that's coming in can be on any of those ThreadPool threads. Am I going asynchronous? It releases those threads until that work is done and more requesting coming in. - Great and to learn more you can check out our documentation. - Right, so the code that we worked through today is based on this tutorial. So it's a seven minute read and there's a whole lot more out there on async and await and task based programming for you to learn. And there we go. - Yeah, so that concludes our async series. Be sure to check out our other series on C-sharp asp.net.core and thank you for watching.

Contents