Visual Studio 2012/C# breaks code execution, no breakpoint set - c#

I'm having a problem in Visual Studio 2012/C# which is driving me crazy. I have a particular line of code in a C# file with NO breakpoint set. Every time this line should be executed the debugger interrupts the execution of my code like a breakpoint was set. The only difference i noticed is the arrow on the left hand side, indicating the current step. Usually this arrow is yellow (when a breakpoint is set). In my case it is grey.
I have been searching for a solution but did not find anything useful to this. I tried deleting all breakpoints, build project, rebuild project, clean project and it still appears.
Does anyone have the same problems and found a solution this?
normal arrow:
arrow in my case:

This line of code is currently executing, it calls something else, e.g. native code or .NET internal code and an exception happens there. You can see both arrows in the call stack window:
Perhaps you need to turn on "Show external code".
If you can't see the exception dialog, usually you can show it like this:

Related

Cannot edit after using 'Break All', although break points work fine

This seems to be a WPF-specific issue.
If I set a breakpoint in the code, the program will pause when it reaches that line, and I can edit/add/remove code, and then continue - it runs the newly edited code (ie, it behaves as expected).
However, if I hit 'Break All', I get moved to window that says "Your App has entered a Break State...". If I try to edit my code, nothing happens. I try to type but nothing happens - no text appears, and there's no errors that pop up or anything.
I don't have this problem in WinForms applications - just WPF. If I create a basic WPF project from the template, I still have the issue.
It's a very frustrating issue! My ugly hacked solution is to add a button to my program's UI called 'Break', which executes a single line of code that has a breakpoint on it, basically recreating the behaviour that 'Break All' should have.
Weirdly, it's not an issue if I'm on a WPF project which uses multiple threads. Hitting 'Break All' in this case acts as if there's a breakpoint on the line of code where the background thread is set up.
I am not sure..but you can give a try.
To enable/disable Edit and Continue. In the Options dialog box, expand the Debugging node, and select Edit and Continue. In the Options dialog box Edit and Continue page, select or clear the Enable Edit and Continue check box. The setting takes effect when you restart the debugging session
source:https://msdn.microsoft.com/en-us/library/ms164926.aspx

ReactiveUI.pdb not loaded

I'm using ReactiveUI in my WPF program, the WPF Window can be initialized and show normally, and I'm under Debug Mode,
but sometimes When I run some command code binding with button, it shows error below, and there're not more information, so I have no idea what's the reason to the Exception:
ReaciveUI.pdb contains the debug information required(...)
and after Continue, a window shows then the program was stopped:
An unhhandled Exception of type 'System.Exception' occured in ReactiveUI.dll
How to deal with this problem?
I found the keypoint of the problem later, the bug happens at
BtmCheckCmd = ReactiveCommand.CreateAsyncTask(
canBtmCheckCmd, //CanExecute
async _ =>//Execute
{
ProgressWinDow.RunCheckAndBarHandle();
MessageBox.Show("SomeThing");
}
I find a new way to check whrere the problem I former don't know so later I could found the solution.
In my case when the exception before happens, sometimes the messagebox show (sometimes not), then if the messagebox show, somtimes after click OK and the Exception Window Show, and if doing like this I can't find which part of my code does the problem haapen,
but if I click pause when the messagebox show(not click OK, but click pause under debug mode when msgbox show), then it go to the part of code it execute,
and by doing this I find the "Messagebox show" is in the code block of
async
in the part of code, and there're two method in async, so it should be the reason to this problem, maybe something wrong when async two method at the same time.
And In my case, the Messagebox is originally just used to test, so I delete the MessageBox and the problem solved.
Another way to find the problem in multi-thread is under debug mode,
and set the break points, then execute that part of code, when going into a break point, then go to the top ToolBar select :
Debug>>Windows>>Threads
then in the threads window can see what are the threads doing
In my case Visual Studio (2022) had locked a file.
To locate the locked file I did (in a console at the root of my project):
git clean -xfd
This gave me a message about several files which VS was locking.
But ultimately, the solution was to kill Visual Studio, perform a git clean, and then rebuild. You may also need to log out if on Windows to release the locked file(s) before rebuilding.

Visual Studio is missing/moving my breakpoints

The problem is that when I place a breakpoint and debug/run, the breakpoint moves by itself.
Before/whilst coding:
After clicking run/breakpoint hit:
Breakpoints typically work ok for me, but it seems that they sometimes randomly play up. I first had this ages ago with a VB project, and in the end, I bypassed the problem by removing the breakpoint and adding it somewhere else where it was still useful.
Whilst I could probably do the same again, and this is only the second time it has happened (that I remember), I don't really want to have to and would like to know what is actually wrong.
I have read through many similar questions here, but I cannot see an exact match and the answers do not help. I have tried - building, rebuilding, closing/reopening and cleaning.
I only provided a picture of the bit of code where it occurs, if you need anything else, please let me know.
It's because the debugger isn't able to break at that point. For example, the debugger can't break on auto-implemented properties, or at the header of a method; instead, it breaks at the first line of the method.
Also check to see if you had set the breakpoints in one mode (Debug) but are now in a different mode (like Release). This caused me some momentary angst.
I got this problem after Windows had inexplicably added 6 months to the current system date (and 1 hour to the time). I didn't correct this right away, so builds made before the correction looked more recent to Visual. This lead to running the wrong (older) build when debugging.
Since I couldn't find a "Clean Solution/Project" option, I had to manually delete all .pdb files in the solution. The problem was instantly fixed. I just hope there aren't any more files I might need to delete (I'm new to Visual and don't know much about how it works behind the scenes).
You can get this if the breakpoint is AFTER a return statement. Visual Studio will bounce the breakpoint to the closing brace (or "End Function" for VB.NET) because return is jumping out of the function.

Debugging C# applications

I´m coming from a PHP background where my debugging "tools" are basically echo, var_dump and exit. While I know the importance of debugging, I never tried to use/learn a debugging tool.
Now I´m learning C# and I think I can´t really program without an extensive knowledge of this area.
So my question is: where can I learn what is and how to do debugging? I know what is a breakpoint (conceptually), but how to use it? How to use Step into and Step over? Basic things like that.
As a related question, there is anything like var_dump in C# (Visual Studio), where I can inspect any object.
I find very difficult and painful to do a foreach for every array/list to see the contents, specially now that I´m still learning the language.
Microsoft has an extensive guide on C# debugging in Visual Studio that might be helpful. VS has a lot of powerful debugging functionality; for example, rather than doing a foreach to see the contents of an array as you were describing, you could set a breakpoint (pausing the execution of the program) and select the variable you wish to see the contents of (array or list or whatever) and see what it contains, without having to write any extra code. Step Into and Step Over can be used to continue execution of the program but only incrementally so that you can continue to see how variables change, where the flow of execution currently is, etc.
This has been covered earlier on StackOverflow:
Best Visual Studio 2008 Debugging Tutorial?
So, your compiled applications can be run in a "debug" mode from which visual studio can monitor the internal workings of the application and even control it.
A break point can be placed just about anywhere in your code by clicking to the far left of the line (kinda in the margin of the visual studio text editor). When that line of code has been reached, the visual studio debugger will actually pause the execution of your program and bring you back to the editor where you can literally hover over a variable or object or whatever and see everything about it.
There is also a "Locals" window available that will give you the break down of all of your locally scoped items - this should pop up by default at the bottom of your screen when debugging.
In debug mode you can navigate the execution of your code line-by-line
F10 will continue with the next line of code.
F11 will attempt to drill down into what ever functions are on the current line of code
Ctrl-D will bring up a "Quick Watch" window giving you all information about the currently selected variable/object.
Once you are in debug mode there are tons of things you can do - in some cases you can even edit the code as you go.
The easiest way to get into debug mode is to use the little "play" button up at the top of visual studio - and when a break point is reached it will enter debug mode and highlight the currently executing line of code.
You can also hit F10 from the editor and your application will be started and paused on the very first line of code.
By comparison, in PHP, you had to actually write "debugging code" into your application - using Visual Studio you can actually monitor the execution of your code without adding a thing to your existing code.
I hope that gets you started.
You might want to also read up on your IDE a bit to. There is a metric ton of stuff in visual studio that will help you navigate your code in ways you never imagined in most PHP editors.
If you've already downloaded Microsoft Visual Studio, you'd might want to check out the Visual C# Express Library available for free over at: http://msdn.com/express/
It's located down the bottom of the page and is very useful. It contains pretty much every answer you might be looking for as a beginner to the C# Language. ...Welcome to C#, my friend :-D

C# program quitting/crashing suddenly, how to break it?

So my C# program is quitting/disapearing/closing/crashing suddenly even in debug mode.
How can I make it break/pause/stop so as to find out where caused it?
Set a breakpoint (by clicking the left of the line numbers--it's a red circle) that you know is before the point where it crashes and then step through it (using F8). The last statement that you're on is the one that it's crashing at.
If you're in Visual Studio you can enable "Stop when an exception is thrown" - go Debug->Exceptions. This is often quite useful for detecting the exact source of the problem.
You may be able to learn more by using an unhandled exception handler. However, if you're in debug mode you should be able to see it by default. Check your Debug: Exceptions settings to make sure you haven't accidentally stopped reporting them in debug mode.
as Eric suggested try going through step through it... also always place try/catch in your end caller apps in order to manage exceptions.
Also, it may be worth checking your code for occurrences of Environment.FailFast, just in case someone tried to be (too) smart.
You can hit F10 to start debugging your project. This will put a breakpoint right at the beginning of your Main method, then you can go from there stepping through the code with F11 to see at which points it crashes.

Categories