In this video
will try to understand
how we can debug threads in C# using visual studio
In this program we have a very simple method here called as my method
This my method has a for loop which actually runs 10 times
While this for loop is running
it actually goes and manipulates a simple variable "i" it just increment just variable i as
it goes and runs every for loop
and after it manipulates this variable "i"
it actually goes and halts that thread for five seconds
Invoke this my method
by creating two threads
Create one thread called as One
Create one thread called as Two
He also invokes the MyMethod
This MyMethod is invoked via two threads i.e One and Two
Put a Console.Readline
This static void main thread is halt to see the outputs
Debug this console application
Put a small debug point inside this for loop
This variable "i" is manipulated when
it is manipulated by which thread etc
The debugger has now come and it has halted at this debug point
and we are able to debug
This debugging has problems
The first problem is
We are not able figure out
debugger is debugging which thread
This MyMehthod is called via two threads
One
and two
We are not able to figure out debugger point is debugging one or two
Click on debug -> Windows
There is a menu called as Threads
It open up a big pandora box
This pandora box
gives a complete insight of
what is happening inside all the threads
This yellow sign says
this where your current debugger is
debugging
This MyMethod is been debugged by Worker Thread
When the application runs
It runs on thread
that thread is main thread
This green sign and the Main thread
signifies this actually runs on static void main
There are lots of worker thread, they are background thread
From these worker thread how to figure out which are our thread and which are threads which are by the console application
The last two workers threads
location is MyMethod
We can figure out these two are our worker threads
i.e one and Two
These are the threads which are used by the .NET run time
When we debug threads, we should be concerned with only two kinds of threads, one is
the worker threads
which are attached to MyMethod
The second one is the main thread which runs the application
Both of them are having this name as no name
Whenever we create threads
If we want to ensure the debugging is smooth
Name the thread
We are able to distinguish this is One
this is two
Naming the threads is a very good practice
Specially when we are debugging Multithreading application
When the debugger is debugging a thread
All the other thread will also go in a halt mode
The debugger point is on One
The debugger has now
stopped the application
at MyMethod
at the same time
all the other worker threads including the static void main even they are halted
Select over Two
and double click
The two is currently over here
When the debugger is attached or it is debugging one thread or if we have paused
the debugger at one point
All the threads will also be paused at the respective points
We would run into a scenario where
we would like to debug one thread
While debugging the thread we would like to pause the other thread
Right click on this thread
If we click on freeze
the debugger
will only allow
or it will only debug one
This Two is not running at all
It is just running One
To revoke the same
click on the thread and click on
Thaw
After click on Thaw two starts running
This freeze and thaw will use when we want
one thread to be stopped and
and debug the other thread
When we put the debug point here and run the application, we can either halt on thread one and either halt on thread two
Create this debug point as a conditional break point
put a condition here
Now if we run this, do a F5
The debugger point will only halt when the thread is two
Its only going in a debug mode
By naming the threads
How easy the debugging becomes
In this video we discussed some important couple of points
How to debug a Thread
Click on Debug -> Click on Windows -> Threads
What is the importance of naming a Thread
We also saw the Freeze and thaw by which we can halt one thread
and by which
invoke
the thread whenever we want it
Isolated debugging is possible by using Freeze and Thaw
We also saw how to Conditional Debugging by using
the name of the Thread