Timer inside .NET MAUI App (System.Threading.Timer)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone welcome back once again in this video we will look at timer inside the net Maui application using system.threading.timer you will try to display the current time every second using the main thread UI code so if you haven't subscribed to the Channel please let me do it because I've got lots of videos coming up okay so let's get to it then so as you can see I've got a visual studio 2022 Community office yeah to get us started let's go ahead and create a new project so go to the create new project dialog it might take a while okay so here as you can see on the our project side yeah I've actually selected Maui so basically if you don't have the the net mile we installed please make sure you go to the installer and it install all the necessary components I've got a video on that so I'm not going to cover that but if you don't know how to go about it yeah please check on the channel I've got a video on that and follow the steps yeah so once you're done with that video then you come back to this video okay so as you can see here I've got a couple of Maui apps here but we're going to use the the net Maui I mean dot net Mario app obviously we've got that memory Blaze app and whatnot yeah so this is just a project for creating a net marry application for iOS Android market list when you are investor okay so we select the first bit we click on the nest so here obviously you can configure your project details like the project name and whatnot yeah we're not going to look into that so we're just going to go ahead and click on the list so here obviously is layer the next framework that is applicable to you yeah it might for this video sake we're using.net 7.0 so I will click on the create so this might take a while for the project to create so I'm just going to pause the video why that also increase well it's already created so there's no need to pause the video so what I'm going to do is I'm just we're just going to go through this um this uh template and remove a bunch of stuff from it yeah so we can start with the main page.saml so here as you can see you've got a very like sample stuff here so we're just going to remove the image and the first label so you just left with a another level in the bottom so let's remove the other label as well and let's leave this label only so basically this is what we're trying to achieve I'm just going to I'm just gonna put Android emulator Yeah so basically I've got the time here so actually it's going it's running like a little more time so basically um every second the daytime.utc now will be actually called so to display the current time yeah so I I mean I was actually doing it I mean doing a while the video was impossible so we're just going to start from here so I will just make an I'll just make changes to the videos so here we're just gonna put that we're just gonna subscribe to the the loaded event so this Got Loaded yeah so this will be when the page is loaded so we're just gonna put some more tab here this will create this event handler so we're just going to name it to our current time okay and we press enter so this would like to create this method current time method so we're just going to remove the front new and um not implemented as such as from it yeah so from here what we do is uh so I'm just gonna what we do is just gonna build the application the project first so we right click the project and rebuild the project so I'm just going to pause it so now we have the the project built now so let's go ahead yeah so uh let's use the main thread you have to write to the UI so it means the main thread class allows application to run code on the main thread of execution and to determine if a particular a particular block of code is currently running on the main thread okay so it's amazing thread dot begin invoke begin invoke on Mains right okay so this is an action so let's so this is how you write it yeah so inside it this is what we're going to do so inside it here is going to start with a new variable timer this will be equal to a new system dot ready the timer not timeout timer okay so here 's what we're going to do the first bit is the Callback yeah this is the method that we're going to run our time inside our daytime inside so we start with a obj so object and then the method okay and then the next bit is the object parameter that we can pass to the Callback yeah but we're gonna make it now and then our due time the due time we're going to use some time span Dot from second so we're just gonna make the due time every second okay and then the time interval will be a times fund Dot from second as well second uh we're gonna make it uh one second as well okay so this is what we have for our title so inside this callback here this is what we're going to write so here we're going to write the main thread again the main thread that evoke name main thread async excuse me so here this this and actually so we're just gonna put this so this is what we have here so in here we are all we're going to do we're just going to pull up the time now see this this bit here um this time now here the test we just we're just going to change the test here so it's a time now DOT test will be equal to um daytime dot UTC now to screen so here we're just gonna put the capital T for all the time so forth for a long time okay so this is all we we've got to do yeah so right now we're just going to go ahead and run the application and see what actually happened but here we're going to change the font size to like let's say 24. and first of all we just we're gonna rebuild that so I did before we run it so right now the build has completed so let's go ahead and run it so let's run it on an Android emulator so I'm just going to select the Android emulator here so let's go ahead and run it so I'm just going to pause the video because it might take a while yeah to run it for the first time so as you can see here now we've got um the project running and and every second the the daytime that UTC now is called and the time that the current time is displayed in seconds okay so as you can see we've got the time changing every second yeah so this is how you use the system got trading the timer as well as the main thread up the healing book and whatnot inside a DOT net my way application yeah so I'll leave this video here so I hope you got it and and I hope you subscribe to the channel as well so please have a lovely morning afternoon evening wherever you are peace
Info
Channel: Hacked
Views: 3,975
Rating: undefined out of 5
Keywords: maui, .net maui, .net maui android, .net maui ios, .net maui winui, system.threading.timer, visual studio 2022, .net maui mainthread, mainthread
Id: vb-VVMezr8s
Channel Id: undefined
Length: 9min 53sec (593 seconds)
Published: Sat Aug 19 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.