Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / programming / threads

Watch Value of a Variable Across Multiple Threads

5.00/5 (2 votes)
7 Sep 2017CPOL1 min read 6K  
How to watch the value of a variable across multiple threads

Everyone is well aware of various ways of watching value of variable during a debug sessions. Watch, Immediate, Local Variables are just few examples. When working on a multi-threaded application, there may be scenarios that a method is called by different threads which means that a variable may have different values across different threads. If you want to watch the value of that variable across multiple threads, the conventional tools may not be too helpful. This is where Parallel Watch window can come in handy.

Let’s consider the following piece of code. The relevant piece of information here is that CheckIfEven method is called using TPL AsParallel construct.

The method CheckIfEven takes an int variable input as parameter that we want to watch. We can watch its value via traditional Watch window but let’s try to do it by Parallel Watch window. This could be found under one of the Debug menu options as shown below:

Next, we add a watch here. You can do it clicking on the “Add Watch” text and entering the name of the variable.

Now once your break-point is hit, you will be able to see the value of this variable across multiple threads.

Until next, happy debugging!!

Filed under: CodeProject, Debugging

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)