Reputation: 1729
I am running a C# console application which for example is the following:
int sum=0;
for (i=1;i<100000000;i++)
{
sum+=getSomeAlmostRandomValue();
// if (i%10000==0)
// Console.WriteLine(sum/i);
}
In simple words, i want to check the average of the results for a big number of iterations.
At some times, i want to check the average so far, to see how it is going... But checking the IF condition every time in the iteration, is probably too dumb/time-consuming. (This is just an example to introduce my problem. I know that i can break the for loop into 2 nested for loops in order not to have the IF. But still, maybe i don't want to WriteLine every some iterations). So my real question is the following:
Is there a way (by deleting the 2 commented rows) to pause the program at will and check the value of my variables (i,sum)
Upvotes: 2
Views: 1548
Reputation: 64467
This looks ripe for a unit test that can assert the correct values, completely negating the need to periodically check manually, but I can't help you there because I don't know what this is trying to do.
In this particular example, I would use a conditional breakpoint in the IDE.
However, if you want to do it in code, you can break using the System.Diagnostics.Debugger
class, Break()
method.
There are also helpful methods found in the System.Diagnostics.Debug
class, such as Assert
, Fail
, WriteLineIf
, etc.
It would look something like this:
int sum=0;
for (i=1;i<100000000;i++)
{
sum+=getSomeAlmostRandomValue();
if (i%10000==0)
System.Diagnostics.Debugger.Break();
}
The problem with Debugger.Break
is that it will compile into release code, whereas most (if not all) of the Debug
class methods are conditional on the DEBUG
compiler symbol. This problem can be side-stepped using your own conditional code:
#if DEBUG
Debugger.Break();
#endif
However, I tend to err away from leaving this code in the code-base once the debugging session has finished. That said, sometimes you'll find that leaving a little debug infrastructure around some troublesome legacy code can come in handy in future.
Upvotes: 0
Reputation: 1729
Thank you all for your answers,
You got me into checking debugging options and i found exactly what i needed.
What solved my problem was the "Break All" button. When i press it, the program pauses and i can mouseover on the variables and check their values.
But I use C# Express 2010 and the "Break All" was not on my toolbar. So this is how i enabled it:
Tools -> Customize -> Commands -> Toolbar -> Debug -> Add Command -> Debug -> Break All
Upvotes: 0
Reputation: 39
If this is an application where you aren't in an development-environment you should look into the producer-consumer pattern. With it you can pause your application while also securing that the data will be produced fully before displayed.
Upvotes: -1
Reputation: 16277
Conditional break point is your choice.
Right click on the break point, set the conditions there. See also MSDN blog here.
Upvotes: 5
Reputation: 56429
Just use conditional breakpoints and debug the application. For example, your modulus logic can be used as the breakpoint condition:
i % 10000 == 0
Which will hit the breakpoint for every 10,000 iterations.
Upvotes: 1