hmm, I feel my OP title sounds wrong, but I am not sure how to put it...
I mean something like during the runtime, whenever a button button being click, dialog prompt up, any method being called etc. There will be some output that print out my StackTrace or something that indicates where I am in the code.
The reason being is, I just picked up a new project with very huge source code (62 projects in a solution), so pretty much I always have no idea whenever a Dialog or a View open, where the file is or a method is locate. So I want a good way to keep track on where I am, tell me which files or at least the method that calling it. Then I will know where to set a break point in VS.
But as I stated, the projects is pretty huge, so there is no way I can go to every Class and every method to add Debug.WriteLine("Method XXX being called").
So I wonder is there any way I can make a piece of code being called whenever any method being executed? i.e. Some event handler that will execute whenever method is being call? Or does Visual Studio have a functionality that can help me trace where I am in the code? (i.e Button being click, the last return line is XXXX)
Use a profiling tool.. Try "Ants"
A few things come to mind:
Apsect Oriented Programming
I've used this to weave in code at every method call to do extremely deep tracing. I happened to use Postsharp.
Intellitrace
This is a pretty handy tool from MS. It looks like you can do method call level logging.
Related
I was wondering is there a way to check what methods were recently called, and the parameters of them are? I seem to find myself having a lot of everyones in methods, but it only tells me the line that it happened on, I use nlog to log my exceptions, and then I go to the client and file it was logged on, but I constantly find myself having to Console.WriteLine() before every method to know what parameters were passed.
Is there an easier way to do this?
I'm in a curious situation whereby I've realising if the set breakpoint on my code then it will be triggered, otherwise the method is never called. My question is; how would setting a breakpoint on alter the way C# code would interact with a WinForms application?
The culprit seems to be ScrollableControl.ScrollControlIntoView Method. I've set the Flowlayoutpanel's autoScroll property to true, and the vertical scrollbar is visible but it still makes no difference.
The call stack would make it obvious. Having to guess without one: yes, the debugger can certainly affect GUI code. Particularly so by setting a breakpoint. It is a side-effect of the focus changing from your window to the Visual Studio main window. And back. That affects code that subscribes the windows' De/Activated events, Got/LostFocus events and any code that involves painting if VS overlaps your window.
This can certainly get in the way when debugging GUI code that depends on these events. In extreme cases you may need to setup the remote debugger on another machine so this focus switching doesn't happen while debugging.
ScrollControlIntoView() is associated as well. That normally happens automatically when a control acquires the focus. This roughly answers your question, hard to see how it could be useful to actually solve your problem though. Be sure to look at the call stack to get more insight.
It depends on where exectly this code is. If it's in some event-handling method (say resize), or, say, painting method it could alter behaviour of your application.
That's why modern desktop for programmer should have 2 monitors, where on one you run your application on another set your breakpoints. But even in that case, you can meet some problems.
So if breakpoint in the places mantioned before, simple loggging often is more suitable solution for this kind of debugging.
I have a line of code that will take anywhere between 10-50 minutes to do its task which is an analysis thing. The line that is taking this long is housed in a thread however there is no callback implemented in that managed library hence I can't let the user know where we are in parsing this file.
Is there any way to delve deeper into a library by using reflection? I know that didn't sound right as reflection gives you information about the libraries types and code but does it do anything useful during runtime about external libraries? I hope the question was clear.
Thanks,
Without callbacks you're probably going to be stuck estimating the time left based on some other observable side effect.
Would it be valid to track the time taken for previous runs and estimate progress based on that?
If the output is to something external like a file/db could you inspect that?
If the output is something in memory you could inspect the object in another thread if you created it or use some "unsafe" code to inspect the memory if you didn't.
You can use http://www.reflector.net/ to see what the code in the library actually does, but unless you find any callback hooks, then there's not much else.
How is that setting a breakpoint in my code allows the following code to complete which would fail otherwise.
Here is the problem.
I'm writing an add-on for SAP B1 and encountered following problem.
When I load a form I would like to enter some values into the form' matrix.
But without a breakpoint (set on a method in which loading a form takes place) the part of code that is executed afterward will fail. That part of code is referencing a matrix that is not yet displayed which results in an exception. This is all clear. But why setting a breakpoint "solves" the problem.
What is going on?
I suspect that my breakpoint introduces some delay between loading and displaying my form and part of code that references element of that form but I could be wrong.
Running under the debugger does slow your app and will often hide race conditions even without a breakpoint. When you introduce a breakpoint, it is even more likely to hide race conditions. These kind of problems can be difficult to solve. You might want to introduce some simple logging (e.g. log4net) to see what it is going on without impacting the app so much that you see different behavior. Just keep in mind that even logging can be enough to change things.
Having breakpoints means that every time a module is loaded at runtime Visual Studio scans the module for the positions of possible breakpoints. This must introduce a delay.
Is this a Windows Forms based application? (I'm afraid I know nothing about SAP B1)
Try putting your code into the Load event of the form if it is not already there. Some controls aren't ready to use properly until their handle has been allocated which doesn't happen until the windows message loop has run a few times.
Breakpoints do introduce some delay. A breakpoint is the addition of extra instructions to your programs regular execution. Both hardware and software breakpoints ADD something to the execution of the program (although the amount would widely vary).
http://en.wikipedia.org/wiki/Breakpoint
Sometimes, while I am debugging a c# application, I will hit a break point and when I try to continue, step or step into, it just does nothing. The yellow line highlighting the current line goes away, but it never reaches the next line. The app is still frozen like I am on a breakpoint and I can do nothing but hit the stop debugging button and restart. This doesn't happen all the time, but once it starts on an app it seems like it always happens after that for that app. I have found that adding the following code just before the class declaration "fixes" the problem for that app, but am very curious as to why this is happening.
[System.Diagnostics.DebuggerDisplay("Form1")]
Additional details:
I have not noticed any kind of pattern as to what the particular line does when it freezes. Most of the apps I write use threading, so there is a decent chance this is happening within a thread every time.
I've seen stalling problems where the debugger is trying to evaluate the variables shown in the Auto/Local windows. If the evaluation is complicated then it can cause significant stalls.
You can turn the auto-evaluation off through Tools|Options and it does make a big difference.
I have come across with this kind of behavior, though this is my first time.
I have got through this problem, by two ways
Your way of putting this attribute, [System.Diagnostics.DebuggerDisplay("Form1")]
Turning off Tools->Options->Debugging->General->Enable Property evaluation and other implicit function calls.
I am still debugging my code but It seems to me that some of the Autos evaluation is failing (possibly throwing an exception), which is possibly crashing the debugger.
Please let us know if this is also your case.
What sort of code are you debugging?
When you "step into" are you calling your own .NET code, or calling a native library, or an external assembly that you don't have the pdb files for? Either of these situations would cause the debugger to freeze while the external code was executing.
If you debug multithreaded application you might be changing of thread. You can switch between Thread with the "Thread windows" while debugging to be able to see again where the debug yellow line is.
My psychic debugger says that you're missing symbols for something and that VS is hitting the network trying to look them up. Try setting your symbol path to something weird like C:\foo.
dead-lock seems likely in your case. Press the pause button and look at the threads view next time it happens.
I have seen this type of behavior when my DB was being very slow, NHibernate is trying to write to it under the hood, and the whole debugger gets locked randomly when the DB gets pegged.