Cannot write code in visual studio while debugging - c#

Hi I have been shifted from VS 2013 to 2015 presently. So I am not too much familiar with in depth specifications.
I like VS 2015 as it allows me to evaluate lambda expressions and also it allow to me to change values runtime.
Scenario:
In some apps (winforms tested only) I am able to edit code while debugging and yes latest code is executed always i.e I don't need to stop and run the program again and againe. Yes it is very excellent feature.
But in case of my first experience at controller in MVC I am unable to edit code and it shows me multiple reasons behind this.
Can someone explain in what scenarios I can add/delete code while debugging?
Thanks in advance!

In your visual studio go to Tools->Options-> expand debugging -> select Edit and continue and then check Enable edit and continue.
here is image of that:
see screenshot

You normally cannot edit and continue if you attached to an already running process which maybe your case if you are attached to a running MVC web project. If something was load using Reflection you will not be able to edit it as well.

I have also seen sometimes a similar message appearing during debug. I found the following from MSDN regarding code changes during debugging:
The following changes cannot be applied to C# code during a debugging session:
- Changes to the current statement or any other active statement.
- Active statements include any statements, in functions on the call stack, that were called to get to the current statement.
- The current statement is marked by a yellow background in the source window. Other active statements are marked by a shaded background and are read-only. These default colors can be changed in the Options dialog box.
- Changing the signature of a type.
- Adding an anonymous method that captures a variable that hasn’t been captured before.
- Adding, removing, or changing attributes.
- Adding, removing, or changing using directives.
- Adding a foreach, using, or lock around the active statement.
More information: https://msdn.microsoft.com/en-us/library/ms164927.aspx

So, after alot of R&D i came to know that Edit and continue is NOT available for Asp.net yet. It would be released in the final version of Visual studio 15.
source: https://blogs.msdn.microsoft.com/visualstudioalm/2015/04/29/net-enc-support-for-lambdas-and-other-improvements-in-visual-studio-2015/

Related

C# Visual studio code debug in live & trace it step-by step WPF

I would like to learn and know, how can I examine my WPF application in Visual Studio 2019 in running / debugging mode.
Exactly what I expect: how can I trace my code what step is executed in background if the application running( communicate with port; modifies string and so on...).
I would like to see what happening if I click on the button and it calls methods and those again do something . Like visually trace the behind code.
I used the breakpoints but tell the truth it is not that I am looking for. I would like if the VS can indicate what line is executed e.g. if the textbox changed the code where continues and soo on. I think this way is existing. :)
The purpose of it to find a intermitted failure that I have made (maybe) even if it is not appears in every case and the VS not drop for it an exception. But the business requirement is not allowed that hidden failure/ unexpected slow processing, and I would like to find where it got stuck.
Someone can suggest what I have to use? In VS I also tried the in Debug menu-->Calls, Thread, Task window but these not give me that I want.

C# check to see if property is accessed by the visual studio debugger

I am writing a C# application. I have some objects with some lazy properties that actually do quite some work when you request their values.
I want to be able to check if they are called by the visual studio debugger when debugging and not execute them at all. For example when I click on an object with these properties then it shouldn't load them at all.
How can I do this?
Consider using DebuggerBrowsableAttribute:
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
The docs state:
Never -> Never show the element.
https://lostechies.com/jamesgregory/2009/08/18/debugger-property-evaluation-side-effects/ also briefly discusses the attribute.
Besides the excellent suggestion of #mjwills, if you don't have the source code or don't want to modify it at this point in time, turn the feature of in VS:

Windows Workflow 4.5 Dynamic update using XAML / Designer

I understand that Windows Workflow foundation 4.5 supports side by side and dynamic update of Workflow definition. I found few samples on internet, but all describes Code based approach for updating the workflow. I wanted to make a utility which is used by developer to update the workflow without coding. (using only Xaml manipulation should be our ideal case)
My Utility does the following things,
I take Base Xaml (the workflow definition I wanted to update) File Name and Assembly (V1) from User.
I prepare it for update by calling prepareForUpdate API. and save it as a ForUpdate XAML.
I remove Base Xaml From visual Studio project.
I load that updated Xaml File in project (instead of base xaml) ,and modify the workflow.
After modification and rebuilding, I again open my update utility and take input from User the modified Xaml (v2) and Create update map.
After that I tried to update the running instance (using update map), At that time my utility show that they are updated successfully.
Although they are updated but at run time make undesirable effects (I got exception like {ICompiledExpressionRoot for compiled location 'a'}, where {'a'} is my variable) , related to Expression parsing, Root , etc.
Is anyone trying to achieve same thing or has some idea. my errors are consistent in both Visual Basic and Visual C# based XAML.
While we have not run into your specific scenario we have attempted to use Dynamic Update services to come to a solution much like you describe.
Despite having mostly declarative workflows designed entirely in the Visual Studio visual editor we abandoned this tool. In most cases this it was unable to generate an update map, sometimes even throwing parsing exceptions.
Using it really did not feel like it was a fully completed component and the lack of documentation around it, even after so much time has passed after its release, makes me very wary of considering it for anything.

Is it possible in Visual Studio to write code or debug an application while it is running?

As the questions says, I want to write code or debug an appication in real-time without setting breakpoints or pausing/restarting the application.
For example, when I write a game, I want to see what is happening when I change the code for the calculation of the light effects or the AI of the enemies immediately, while running the game on my second monitor.
Update:
Ok, it seems that you guys don't understand exactly what I want.
I want Visual Studio to be more like a WYSIWYG editor...make changes or add new code and see instantly what has changed in my application, without the application to pause it's work.
Update:
I saw this feature in this Video with Java in Eclipse (go to 14:30, where he changes the light effects of the game without stopping it.)
Sometimes. Check out the Edit and Continue feature: http://msdn.microsoft.com/en-us/library/bcew296c%28v=vs.80%29.aspx
Based on the comments, it sounds like you either want a dynamic language (a lot of games are scripted with LUA, or check our IronPython or IronRuby) or you want to dynamically load and reload assemblies, which would require something like MAF perhaps. With that, you could build the bits that you are changing as addins, and then unload and reload the addin assemblies when they change. That seems hacky though, and will likely perform poorly compared to a DLR language.
here is all you want to know abt the Edit and continue feature in Visual Studio:
http://msdn.microsoft.com/en-us/library/bcew296c(v=vs.80).aspx
You can edit the code while debugging, but no instruction will be executed during this time.
If you hit F10, the next instruction will be executed. If you hit F5 the normal execution will continue.
Why not create a resource file with the values to apply. Then have a command you can execute in the app that will reread the file. World of Warcraft has a feature like this. /reload ui
Yes, but unless Edit and Continue is enough for your need you need to design and implement the functionality yourself.
if the change is data driven - just reload the data when some file changes.
if change is in code - consider making that portion of the code to be in separate assembly and dynamically load and rewire the assemebly (may require strongly signed assembly to proper version code). Or dynamically compile code into new assembly (to avoid assembly conflicts in the same app domain).
In all cases you need to figure out how to deal with loosing part of previous state that could be in older objects.

Viewing variable values at runtime in VisualStudio

Is there any tool I can use to view the values of variables live as the code executes in VS?
Right now I can see them only when I keep a breakpoint.But,the problem is that the code works perfectly fine when I keep a breakpoint.it messes up only when it runs fast.
Any help would be appreciated.
Thanks
You can only view variables when you have a breakpoint, however you could just manually write the variable values to the Visual Studios Output window:
System.Diagnostics.Debug.WriteLine(variable);
I my opinion , rather than setting break points, you can use Debug.Write(yourVariable) under the Debug mode, so you can watch the value in the output windows.
Cheers.
Sounds like you need a conditional breakpoint. Aside from printing the values (console, debug output, trace) as the code runs there is nothing that will show you live data slow enough for you to see it. Put a conditional statement in detecting when the values are no longer valid and stick a programmatic break point on that.
Use Debug.Write to print out the variables. You can also use profiler.
All the aforementioned methods (dumping to console, using Debug.Write, custom logging, etc. etc.) to dump the contents of variables will do the trick .
From your problem description however (i.e. "...works fine with breakpoints, fails when left alone to run..."), it sounds like you have a threaded scenario with synchronization issues. If that's the case, inspecting the synchronization methods used might yield better results.
In visual studio 2010 (maybe in earlier versions, I didn't check) the conditional breakpoint can be set to print a variable value and continue running. It is done by right clicking the breakpoint and choosing 'When hit'. Then it opens a dialog in which you can specify what and how to print.
It worked fine for me in a native C project.

Categories

Resources