‫Welcome back.
‫In the last video, you saw how to use threads and how you can set up threats and simply start them.
‫And in this video, we are going to have a look at completion.
‫So we want to use a thread to do something and only if a specific task is done, then we want to go
‫ahead.
‫So let's have a look at that.
‫I will create a new variable called task completion source and therefore I need new task completion
‫source and a boolean.
‫So as you can see, it's complaining because it doesn't know what this task completion source is.
‫And in order to fix it I need to use sys threading tasks.
‫So I'm going to do that for a second.
‫And then here.
‫What I'm going to do next is I'm just going to create a test variable.
‫I'm just going to call a task, which is a task completion source, or we just call this task completion
‫source.
‫And we set its task to the result.
‫So we want to have the result of that task.
‫So let's say there was a specific task that should run and do something, and then we want to see the
‫result.
‫So if it worked out or it didn't work out and to test that, I'm going to get rid of this red line here.
‫So I'm just going to run the code as if there is nothing else in this main method, only this code here.
‫So I'm going to run it.
‫And as you can see, nothing happens.
‫So the console is kept alive or open, even though we don't have this console red line, which we usually
‫always use if we want to keep the console open, that means that there is something that is not finished.
‫So this here is never actually finished because there is no result.
‫So this completion source is never actually complete, so it's never set to true.
‫And that's something that we can do manually.
‫So let's say we have a task and we want to have the result as we have it here, but we actually want
‫to finish that task at one point.
‫So what do we need to do for that is to create a thread.
‫So I'm going to create a var thread
‫is equal to new thread and that thread will not be started straight away.
‫So I'm using a little different approach than we did here.
‫So here we just said new thread and then we had the body of the thread.
‫So to code that should be executed whenever this thread is initiated and we initiated straight away,
‫we started straight away.
‫But here we only create the thread and we can do stuff with it later on.
‫So here I'm just going to say thread sleep.
‫So just sleep for let's say 1000 milliseconds and afterwards try to set that task completion source
‫to true so tri set result.
‫What that will do is attempt to transition the underlying task into the task status ran to completion
‫state, which means it's done right so it's going to try to set its done to true.
‫So that means that this task completion source will be set to true.
‫And only then we will get this VAR, which is the result of that task.
‫So let's create a little.
‫Step in here.
‫So debug step and let's check the thread IDs.
‫And now in order to start the thread we actually need to call thread start.
‫And as you can see this thread object here which is a thread which will do all of that is going to be
‫started.
‫So I'm going to start the thread like this and while we're at it, let's have a look at what thread
‫has to offer.
‫So whenever you create a thread object, there are multiple properties and methods to use.
‫Or we can use multiple ones here.
‫So you can see a board is one where you can abort the thread so you can stop the thread, but that will
‫run an exception or it will cause an exception.
‫So that's very dangerous to do actually.
‫So you can crash your application with that based on how security have built it.
‫But a board is something that you can of course use to stop a thread, but you never know how far it
‫is in its execution.
‫So it can happen that it is not done with the tasks that you wanted to be done with and that will cause
‫to, well, your program not working correctly.
‫And then of course, there are plenty of other methods that are pretty interesting like or properties
‫like this is alive, which means it's this thread currently alive then is background, which means we
‫can say that a thread should run in the background and we can check with this property.
‫Whether it's in background or not is a threat to thread.
‫That's something that we will check out later on.
‫So what is a thread pool?
‫That's what we'll see later on and so forth.
‫So the managed thread ID is something that is pretty interesting, which will tell us about the ID of
‫the thread that we are currently running.
‫So let's use that, let's print that onto the console.
‫So see W and I'm going to use the thread number as this.
‫So you use the dollar sign and then you have the thread number or thread ID and so you can call it.
‫And then in curly brackets you use the thread managed ID, so.
‫Thread managed thread ID.
‫And of course you need to close the string.
‫All right, now let's test it.
‫Let's run the code.
‫And we are we have our main thread.
‫And so far we have something on the console.
‫As you can see, thread number is three.
‫So the number of this thread that we have here that we've created ourselves is actually three and the
‫main thread it's managed ID is one.
‫So as you can see, the numbers don't actually have to be in the manner that you would expect them.
‫So even though we haven't created our own additional thread in here, it goes from 1 to 3.
‫So there have to be other threads that are running that we don't see here.
‫All right.
‫And by the way, if you want to see whether a thread has started and end it, you can do the following.
‫So.
‫So let's cut that here and put it in this line here.
‫And actually, let's say started and here.
‫We cannot use thread because thread is this thread that we create here.
‫But if we want to use it in here within the thread body itself, then we need to use thread dot current
‫thread.
‫So if we do that now, we will have the thread that we are currently in, which is exactly that.
‫And here we say it started and after it's done we say it's ended.
‫And now let's run it.
‫It's run the code.
‫And as you can see, nothing is displayed on the console.
‫Let's go to the next step.
‫And then it started and ended.
‫So it happened quite quickly.
‫But as you can see, there are.
‫So let's give it a little more time.
‫Let's change it to 5 seconds.
‫And let's also get rid of this debug point here and let's run it.
‫So thread number three started and then it took a while and it went to finished.
‫So now you see that this line of code is actually executed correctly and it simply stops our console.
‫So let's get the console read line to keep the console open.
‫So thread number three started and then 5 seconds later it should end so that we are thread number three
‫ended.
‫Now let's say we want to see the task actually worked out.
‫So if it worked out to set it to true or not.
‫Well, we know because otherwise we wouldn't get to the next step.
‫But let's use console right line in here.
‫Task was done and here I'm just going to write the result which will be test.
‫So test is just a bowl, as you can see here, because either it worked or it didn't work.
‫So we sell this either true or false.
‫So let's check it out.
‫And now we need to wait for 5 seconds.
‫Number three started 5 seconds and then number three ended and it says task was done is equal to true.
‫So that's what's happening here with this line.
‫So it simply says it's done with this specific task.
‫All right.
‫So in this video, you saw how you can use the task completion source, which indicates whether a task
‫is done or not.
‫You can set it to true or it can simply do something and be done.
‫After a while, you also saw that you can create a thread in a different manner, so you can actually
‫create a thread variable, which you then have to start of course, and you can simply use like an object
‫of the class thread.
‫And then we saw how we can use the manage thread ID so we know the ID of that specific thread.
‫And that's something that we'll look into a little more in the next video where we create multiple threads
‫at the same time and then we will see how it's going to be handled by our machine.
‫So see you in the next video.