Debugging Window applications - c#

i have windowapplication project which contain so many forms ,,i need to
debug the application ,,but i dont know which code is related to which
UI window,..Is there any way to find the code which correspond to related
window

Open the windows and press 'F7'
Right click on the form and say "View code"
Right click the form in Solution explorer and say "View code"
If you mean other files not related to the UI you can do a "Find all references" when you right click on a specific function. This shows you all places where this function is called.

I think you're looking for an easy way to locate the code for a particular form easily without having to go through the entire codebase to determine what code goes with what form. Unfortunately, there are no easy ways that I am aware of. However, there are a couple of things that you can try:
If the form you're looking at has some sort of a lengthy operation and you're quick enough :-), you can trigger that operation and immediately do 'Break All' from the debug menu or hit 'Ctrl+Alt+Break' to break the execution right there and examine the stack trace.
You can profile your application e.g. performance exporer in VS2008 and then examine the profiler reports to get some hints as to what code is getting called. This may pinpoint or at least narrow down the code you're looking for.
Just a couple of ideas that might helpful.

Related

Is there a way to stay on current document after a "break all" in Visual Studio?

Visual Studio opens source code on top of the stack when I "break all" while debugging; I want to keep the cursor on the document I'm currently working on, without any other document or window (e.g.: no symbols loaded) being opened.
There is a way to stay on the current document, but that requires creating a Visual Studio add-in and a new UI command in the Debug toolbar. Credits for this answer should actually also go to openshac, who posted a similar SO question and also gave a workaround in his OP by using a macro.
The implementation is fairly simple (it took me a few minutes to have it working). First, in the add-in project, modify the Exec method in the Connect.cs file like this:
public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
{
handled = false;
if(executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
{
if(commandName == "BreakInCurrentDocument.Connect.BreakInCurrentDocument")
{
// here's where the magic happens
// ******************************
var activeWindow = _applicationObject.ActiveWindow;
_applicationObject.Debugger.Break();
if (_applicationObject.ActiveWindow != activeWindow)
{
_applicationObject.ActiveWindow.Close(vsSaveChanges.vsSaveChangesNo);
}
// ******************************
handled = true;
return;
}
}
}
After creating and registering the add-in, just:
click TOOLS on the Visual Studio's menu
Customize
Commands
Choose the "Toolbar" radio button
Select "Debug"
Add Command...
From the "Addins" category, choose your custom add-in.
That's it.
Latest build of VSCommands extension (free version) available from http://visualstudiogallery.msdn.microsoft.com/a83505c6-77b3-44a6-b53b-73d77cba84c8 has just what you want.
It adds a Break In Current Document button to Debug Toolbar and Debug Menu:
http://vscommands.squaredinfinity.com/Media/VSCommands/BlogPost//blog/breakincurrentdocument.png
It's a feature.. when you do "break all" then it is assumed that your process has hung. The first thing you might be interested in such case is - WHERE. Hence, it's directing you right down to the 'current' place that is being executed. IIRC, this is defacto standard for all low-level debuggers. If you don't want the "no symbols loaded" just mark the 'show disasembly' and it will never pop up again:) (of course, instead, you will see the exact point of stop. And yes, this is also a feature that I myself used many times to debug unknown library code)
On the other hand, if you know where you want the code to stop, place the breakpoint there instead.
On yet another hand (as if we had three), if you want to actually stop the application - stop it, don't break, just stop.
I sense that your actual problem lies in the fact that you use one of the features in a wrong way, and therefore another feature bugs you. Please tell me, what do you use the "break all" for and how/why does it collide with your current text-editing. Why can't you stop or break-at-here instead? Or "detach"?
Anyways, I have to admit that as a feature, there should be some option for turning it off, just for the sake of configurability of the IDE.
EDIT
AAhh.. you're right. I've completely forgot about the glorious edit&continue. I'm not joking/teasing, E&C is a great feature that I wish all other platforms had. I've forgot about it, because... I extensively use lambdas, generics, foreachs and etc features that effectively block edit-and-continue.
Anyways, the point is, since that the edit-and-continue is the golden feature that you'd like to use - the application must be in 'break' mode. However, nevertheless how do 'break'/'pause' etc it, the IDE will assume it that the PAUSE was you goal, not editing, hence it will show you where did you pause the app.
There are a few options in MSVS like "show just my code" that may help you a little, but it will not solve the problem: edit-and-continue during debugging was designed for "small, local edits". Like, if(x>0)throw new uncaught() instead if(x<0)throw new uncaught(). Your app stopped on assertion or breakpoint and is about to crash, first-change exception handler fired off and here's your chance! You unwind the crash handler, correct the code, then run. Everything in the same one method which you had the stop occur in, as a way of just-in-time patches..
This is one of the main problems why can't you add methods, classes, modify generics, etc during E&C session: ie. editing your current lambda or current foreach might be OK, but the IDE would be not able to relocate the flows and execute the new code properly. This is a bit similar to why you sometimes see the "stale code" warning, but with those code constructs it is even harder to analyze, and therefore not implemented. And probably will never hit the top of MS's TO-DO list :/
The current boom in .Net/C# is not 'live development' but 'notaliveyet development' heavily supported by modularity and unit testing, where you put the effort to be able to test most of the features of the application off-line.. But that's a paradigm shift and for small projects or for local desktop development sometimes it is simply an overkill.

Prevent .net ide from jumping to current executing line when breaking

When breaking in .net, it always jumps to the currently executing line. I would rather just use shortcuts to bring up the stack and go there manually if that's what I want to do.
I could have sworn I saw an option to prevent it from doing this once upon a time but I can't seem to find it anywhere now. Anyone know how to do this?
C# Keyboard layout, when I hit a breakpoint (or halt with Ctrl+Alt+PauseBreak), I usually press Ctrl+Alt+C to bring up the CallStack window, then up/down keys and press enter to navigate the call stack.
With VS2010 I've seen some weird results (options) after installing various plugins and 3rd party components at different work places...

.NET/C# debugging: How to debug a classical heisenbug?

Recently, I've run into a classical Heisenbug. Here's the situation:
I have a tree list, the master view, in one panel, and a detail view in another panel to the right, that displays information about the currently selected tree node. (Very similar to Windows Explorer.)
When I add a new node to tree (think of right-clicking a folder in Windows Explorer and saying "New -> Folder"), the newly created node gets selected.
And here's the bug: The detail view on the right should get updated to show the new node. However, it doesn't. I have to switch to another tree node once, before I can see information about the new node in the detail view.
The bug is easily reproducible and happens in both "Release" and "Debug" build configurations. But: As soon as I set a breakpoint in the event handler (for the "Add new node" menu entry) and enable it, everything works fine =>Heisenbug! I don't have to do any real debugging. Clicking "continue" after the breakpoint gets hit is enough.
For better understanding, I've made a video that should illustrate what's going on.
Everything I could think of to get rid of the issue was putting the thread (the application is single-threaded) to sleep for a few seconds, but that doesn't work.
I'd greatly appreciate any suggestions on what I could try to determine the cause of the issue. Or maybe someone has a guess about the cause?
I'm targeting the 3.5 framework and using x86 as solution platform. The tree list control is from DevExpress' WinForms controls; Visual Studio version is 2010.
Thanks
Update 3: Issue solved. The problem was that the call to the detail view's Focus() method didn't trigger the GotFocus event, which is crucial for updating the detail view. (I don't know why, probably it already had the focus.) However, the method did not fail, it simply did nothing.
Now I just focus another control before focussing the detail view and that's it. The reason why everything worked fine during debugging was that switching from Visual Studio back to my application correctly set the focus on the detail view.
A major obstacle in hunting down this issue was that setting a breakpoint in the GotFocus event handler is useless: anytime you try to switch back from Visual Studio to the app, the GotFocus event gets re-triggered, and you're stuck in an endless loop. Comments on how that could be worked around are welcome.
Anyway, my specific problem is solved.
Thank you very much to everybody who answered or commented. It helped me a lot.
Update 2: In my code, I select the newly created node in the tree. This triggers a FocusedNodeChangedEvent. In the corresponding event handler, I update the detail view and call its Focus() method.
It seems as if that fails when there's no breakpoint set. I think what triggers the correct update during debugging is that the detail view automatically gets the focus.
Window messages
Update 1: Here are the window messages that Eddy's answer provided. (Long list of messages deleted)
First time I've seen a video to support an SO :)
I'm thinking that the right-hand pane, or control group, need(s) to have Invalidate() called - which I think the breakpoint is triggering because it switches to VS, overdrawing the window. Then, when you switch back again, it forces everything to redraw, thus making it work.
When you have a "heisenbug" you need to see what is going on but with a lighter touch that doesn't interrupt the flow of the program.
I would suggest that you form an hypothesis about where your problem lies. I would then edit the code to include calls to the various methods of the Debug class, that test the hypothesis or just give runtime information for diagnosis. Then run your code in debug mode and see what you get in the output.
If you have a more elusive "hiesenbug" theat occurs only in release mode you would need to roll you own logging mechanism to gather your evidence.
Put a System.Diagnostics.Debugger.Break() in the update routine for the other window. Then when the bug appears, a run time break point will be fired and you can view the stack.
It seems that the problem has to do with not getting the right event called to trigger and update to the child panel. When hitting the breakpoint your app loses focus and when you return it gains it again (which is a difference from running it). In order to inspect what events are and aren't called and what the difference is between the two scenario's add the following code to your form to see all the window messages that appear.
protected override void WndProc(ref Message winmsg)
{
base.WndProc(ref winmsg);
System.Diagnostics.Debug.WriteLine(winmsg.Msg + " - " + winmsg);
}
Comparing the differences might help you in determining a way how to workaround this problem.

I make changes to a form in design mode but when the program is built the changes are not there?

I am designing a basic app with multiple forms I seem to be coming across this problem and it will probably be something stupid.
When I make a change to my main form in design mode (like add a button), the button appears in design mode and I can code it but when I build the program it doesn't show up.
Any ideas?
Clean and Rebuild
Make sure you're you're starting a correct form in Application.Run in Program.cs
Most Important of all ..
Save your changes !
And make sure that the build compiles (it might not compile and not ask you if you want it to run the last successful build).
Check out what Microsoft themselves say:
http://vidmar.net/weblog/archive/2005/02/04/999.aspx
The problem was resolved. Just go to taskbar> build >clean rebuild.
Some questions:
If you change the code-behind, does the debugger stop on a breakpoint you put on that change? Also, declare a dummy variable and check if it is visible through the debugger windows such as "Locals", "Autos", "Watch" or "Immediate"?
Did you tamper with Form's default constructor (add parameters, change visibility, that sort of things)?
The form you are changing - are you positive that it is actually a main form (check the Program.Main)?
Does your form include user controls?
Did you try restarting the Visual Studio?
Did you try a full rebuild?
Did you try manually deleting all bin/obj folders then rebuilding?
Is your project actually selected for building under current configuration/platform (investigate the Build check-box under Configuration Manager)?
Did the project successfully build (check the error log)?
Are you running the same configuration/platform that you are building? Are you running the same project that you are building?
Ensure the right project is bold in the Solution Manager or check the start-up project in Solution Manager.
Do you happen to use "Start external program" under debugging options?
OK, this is not exactly an "answer", but answering these questions may produce some clues as to where is the actual problem...

Debugging all events in Visual Studio 2010 without setting break points

I am trying to debug a windows form application which has a large number of events: button presses, timers, etc..
Is there a way to catch every line of code being executed by the application without setting a break point?
edit:
The program was not written by me, so I am unfamiliar with the code. I wish to step through the entire program, catching every line of code being executed. Setting break points in every event is impractical as various controls are created dynamically.
For debugging a button click without setting breakpoints:
Start the app with the debugger.
Get to the state immediately before the intended click.
Go back to the debugger and press Pause then F11 (Step Into) -- nothing will happen.
Go to the app and press the button -- the debugger should take over and drop you into the event handler.
Note: This will not work if Paint, any Mouse event, or probably some other events are handled. The debugger will drop you into those handlers any time you attempt the steps above.
If you're using the Ultimate edition of your Visual Studio 2010 you can use its new feature called IntelliTrace (previously Historical Debugger). This will allow you to do exactly what you want - be able to see all method calls and events that happened during execution of your program, and you'll be able to jump back to the event which you need.
To enable IntelliTrace, go to Tools → Options → IntelliTrace, and check the "Enable IntelliTrace" checkbox, and select one of two modes: "events only" or "events and call information", then run your application with a debugger (F5).
The difference between the two modes is that the latter uses the profiler to collect all runtime information, so you get a complete call stack, however you won't be able to use edit-and-continue features of the debugger.
You can find more in this series of articles, and of course, on MSDN.
You could also try a code coverage tool.
For example, if you have Resharper and dotCover, you can run your application (via the dotCover->Cover Application... menu item) and when the application finishes, dotCover will show you which lines of code were run in the VS IDE by highlighting them in green. Lines of code which where not run are coloured in red.
I don't know if there are other tools which do this, but it's an option.
I developed the Runtime Flow tool to solve exactly this problem - to understand a large unfamiliar .NET codebase via realtime function calls monitoring. It's similar to IntelliTrace, but places more emphasis on control flow than on debugging.
Why would you want to break on every line? This would be very onerous and time consuming. If you want to see the activity of your program as it executes, use a logging mechanism or Debug.Writeline to output information to the Immediate window.
You cannot trace lines of code, but you can use Trace.TraceInformation calls where you want to have an idea of what's executed. There's also Debug.Write. Both output will write in the output window of Visual Studio.
Another solution would be to add logging to your application, for example with log4net, but that may be overkill for your needs.
This isn't exactly what you're asking for, but in case you didn't know you can toggle an existing breakpoint on or off. In your case, you could add break points at key places throughout your code and just disable them when you don't want to debug them. That way, you'll be able to re-enable them later when you want to use them again.
Enabling/disabling is available through the Breakpoints window found under the Debug > Windows > Breakpoints menu (CTRL+D, B). You can also include "Function" and "File" columns in the window, which might help you identify which breakpoints are in the event handlers that you care about
Not really, but you can set one breakpoint and single-step (F10/F11) through the rest of the code.
Nope 'fraid not - you need to set each breakpoint yourself.
If it helps F9 is the shortcut key for assigning a breakpoint - just set a breakpoint on the start of each method and use step through (F10) / step into (F11) from there.

Categories

Resources