Click here to Skip to main content
15,884,176 members
Articles / General Programming / Threads

Watch Value of a Variable Across Multiple Threads

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
6 Sep 2017CPOL1 min read 5.8K   3  
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)


Written By
Architect
Canada Canada
Kamran Bilgrami is a seasoned software developer with background in designing mission critical applications for carrier grade telecom networks. More recently he is involved in design & development of real-time biometric based security solutions. His areas of interest include .NET, software security, mathematical modeling and patterns.

He blogs regularly at http://WindowsDebugging.Wordpress.com

Comments and Discussions

 
-- There are no messages in this forum --