Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In this example, I will look at the watch, locals, call stack, and thread windows inside Visual Studio. I'm inside a project called UsingWatchWindows. This is a WPF application, and I have a little sample UI down here in MainWindow.xaml. Let's take a look at it. There are six buttons in here, and each of these buttons allows me to show you one of the windows inside Visual Studio. And before I show you the actual windows, I need to build my application, and then I am going to attach the Debugger by clicking the Debug > Start Debugging.
Then I am going to click on this Watch Windows button. Notice that I have this code in here that causes the debugger to stop automatically if a debugger is attached. I talked about that in the previous movie. I am going to press F10 to step through a few lines of code. The idea behind the watch window is that it's a permanent repository for data that you want to look at. So if I want to keep this s1 around, instead of having to hover over this to see what's stored in there, I can just take it and drag it down and drag, drop it into the Watch area.
And then when I see the data down here, and I notice that there's an error, I can fix as while I am at the breakpoint by double-clicking on the value in the watch window. Typing in the new value, hi, and it shows it red now that signifies I've just changed that value. Now if I hover over the s1 variable up here, you see that it now contains the new data. So I can edit the data and watch the data. Of course, I can drag more sophisticated objects down here, like this book object, and then I can click on this Plus symbol to expand the details and read more about this class.
I don't have to use variables that I have declared. I can work with the local class. I'm inside this MainWindow.xaml file right now as I am running. So I could come down here and type something in, like the word "this," which refers to the current instance, and then I can read information now about this window I'm inside. Also, I can see information about some of the buttons in here, and I can also use completely arbitrary calls out to .NET framework. For instance, I could say System.IO. Directory.Exists, type in the path to a directory like so, and then it will evaluate that expression and tell me that no, that directory does not exist.
Next I would like to show you the locals and autos windows. I am going to press F5 to finish running the code in here and return back to my UI. Click on that Locals and Autos buttons. Now I'm going to take a look at these two windows here and here. So the autos windows watches your cursor, and as you step through your code--I am pressing F10-- it's keeping an eye on the line of code where the yellow marker is and several other lines of code around that marker. So right now I can see three variables.
If I press the F10 key one more time, you will see that those variables change. Now compare that to the Locals Window. The Locals Window shows me all variables that are in scope inside this method, so that would be the sender, which is a parameter, RoutedEventArgs which is a parameter, plus also these other variables here. Now as I step through my code, I'm going to get to this line of code right here, 68, and that's going to move me in to another function. So as I press F11 now, I have now entered the second function, and you will notice that the Locals Windows changes and shows me only the variables that are in scope inside that function.
Pressing F11 a few more times, I will leave this function and return back to the previous calling function, and then again the Locals Window repopulates with the data from that area. I will press F5 to continue. Next, I want to show you the call stack. In this example I have a function called callStackButton_Click, and it's going to call a function called CreateLoan. So if I go over and click on this Call Stack button--and if you don't see these windows, you may need to go up to Debug > Windows and enable the different windows.
As you can see, there are a lot of other windows I am not going to cover, for instance, there is the Register's window and the Memory window. So here I am. I am ready to make this call. If I look at the Call Stack, I see that I am inside this function. Now I am going to press F11. Now I have entered the second function, and the call stack now reflects that and says, "I am in this CreateLoan," and it was called from this button click procedure. One of the things I can do in the Call Stack window is double-click on the previous function, and it will take me over and show me where the code was where I entered this function.
Then I can double-click on this one with the yellow marker, which takes me back to the current line of code is ready to execute. I will press F11 a few times. Now I have entered another class, and you can see the call stack in now three-deep. And then when I leave this, it goes back to being two-deep. Press F5 one more time. I am going to click on this button. The Immediate window lives over here, and what the Immediate window allows me to do is run one single line of code and evaluate it.
So right now, I would like to call this function, Financial CreateLoan. So I come over here, and I will type in "Fin". There is Financial.CreateLoan. I pass the person's name in, and then I pass in the value of the loan--$5-- and I am going to press Enter, and that function just ran, and notice nothing happened up here, right? I am running this function independently.
It was happening in this window, and then I get back the results from that function. I can also print any details I want by typing in the question mark, which is shorthand for print, and then I can type in something like "this.Width". Here I am referring to the window that I'm inside, and I can find out that I am 525 pixels wide. Press F5 one more time. Next, I want to look at the Thread windows. So I am going to click on this button right here. I am inside this click procedure.
By going in and choosing Debug > Windows > Threads, I can examine how many threads I am running. Now, you may not see the same number of threads that I have here. I have got two threads. It depends on whether you have this thing called the VsHost running or not, which is a special debugger helper. If VsHost is running, then you'll end up with four or five more threads than what I currently have. So I have got the main thread, and I've got one worker thread.
I have another example-- let me press F5--where I am going to spawn a worker thread to go do some extra work. Let's make this a little bit smaller. What I am going to do here is I am going to queue up a thread from the ThreadPool and tell it to go run the code in this OpenTheFiles method. So currently, I'm on the UI thread, because I'm running inside this WPF application. When I get down to this line of code, I will be running on a ThreadPool thread. So I can see the difference in this window. I am going to press F5.
Now I have hit this breakpoint that's inside the OpenTheFiles function. And as I can see in this Thread Window, I'm now running on this thread down here, 1352. As you saw earlier, there are other Debug windows available. For example, there is the Register Window, which shows the data that is stored in the CPU registers. These additional debugging windows are adapted for special debugging tasks and worth investigating.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 105523 Viewers
56 Video lessons · 117178 Viewers
71 Video lessons · 86413 Viewers
131 Video lessons · 41310 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.