VS2013 WPF designer not responding after even simple save - wpf

In my WPF application, whenever I make some changes to XAML, either XAML for controls or resource, after I click save, it takes long time, I mean the VS freezes for really long time, during which both the VS window and task manager show "Not Responding". "Clean solution" also makes it not responding for long time. It can't be that I am running out of memory since I have 24GB RAM and in the Task Manager it says currently only 35% of the memory is being used.
I suspect it has something to do with initializing the design-time DataContext for my UserControl. Because when I only have non-UI files opened I never have this problem. But the worst thing about this problem is I don't even know where to begin to solve it. Can anybody help me!

Related

VS2013 Hangs when Adding UserControl to Form

The best way to explain this is to describe how I can reproduce the problem at will.
Open an existing Windows Form file in the designer
Open the toolbox and click a custom UserControl
Click in the form in the designer to add it
VS2013 hangs (does NOT crash); kill VS2013 and relaunch it
Open the form again
Reset the toolbox and re-add the DLL that contains my custom UserControls
Try adding the control to the form--it works just fine!
Close VS2013
Open VS2013
Repeat the task of adding the custom UserControl to a form
VS2013 hangs
So, the moral of the story is that after closing and reopening VS2013, any attempt to add a custom usercontrol will hang VS2013, and the only way to remedy it is to reset the toolbox and re-add the usercontrol DLL; that remedy only lasts until VS2013 is exited and launched again.
Lots of googling has yielded suggestions regarding using a second VS instance to attach to the first and see the exception that is thrown, but there's no exception being thrown; the application just hangs.
The one thing I found that most-closely described my issue turned out to be related to some remote-desktop software that I'm not using.
Anybody got any ideas? I haven't tried reinstalling Visual Studio, as that represents a fairly significant amount of down-time. Last resort sort of thing (and I've read some things that indicated it won't be helpful).
Edit: Apparently sometimes the reset/re-add doesn't make it better.
Edit: Some additional things I should have mentioned:
There is no devenv.exe when I try to use a second VS instance to
debug, so I end up choosing WDExpress.exe
The particular user control is mature and has not changed recently, and is in use on many forms in the project
The issue only occurs when adding a new instance of the control; the hang does not occur simply by attempting to launch the designer for a form that already contains one.
The latest VS2013 update may have fixed it; it's not happening right now and I don't want to anger the gods by trying to reproduce it again until I get my work done.
Hanging Visual Studio with a custom UserControl is very easy to do. A simple example is all it takes, double-click the design surface and write:
private void UserControl1_Load(object sender, EventArgs e) {
while (true) { }
}
Compile and drop the added control from the top of the toolbox and drop it on a form. Show is over.
You probably did not spend enough time using the guidance you already found or we'd have a stack trace to look at. Attaching a debugger is easy enough and you'd be ahead if the code throws an exception. But it doesn't, a hang like this doesn't throw. Exercise debugging this particular simple hang to find out how to do it:
Start VS again, Debug > Attach to Process > select devenv.exe
Important: click the Select button above the process list. You'll need to select the "Debug these code types" radio button and tick "Managed (v4.5, v4.0)" as well as "Native". Now click Attach.
Give it time to load the symbol files, takes quite a while, especially if that was never done before.
Use Debug > Break All.
If you are lucky then it now stops on the exact statement that causes the hang. Likely in this case. This is however not guaranteed, it might also break on one of the many threads that are running inside devenv.exe, which ever happened to be active at the exact moment the debugger broke in.
In which case you need to use Debug > Windows > Threads to select the thread on which the UserControl code is running. Should be the one labeled "Main Thread". Double-click it.
If the editor doesn't switch to the source code file with the hang then use Debug > Windows > Call Stack to get insight in how the hang occurred. Update the question with that trace if you need more help.
In general, prime candidates for code in your UserControl that can cause the hang are its constructor, its Load event and its Paint event. Use the DesignMode property to bypass dangerous code that is not likely to work well when it runs inside the designer instead of the regular process. And beware of the chicken-and-egg problem, if you have a plain bug in your UserControl then it can easily strike first at design time, before you get a chance to debug it like you normally would. Disabling chunks of code may be necessary to increment towards the bug, use DesignMode to get there.
One more detail, this is tagged [c++-cli]. You must select the old debugging engine to debug C++/CLI code. Tools > Options > Debugging > General > tick the "Use Managed Compatibility Mode" checkbox.
The issue seems to have vanished subsequent to updating VS2013 to 12.0.40629.0 Update 5, found by selecting TOOLS > Extensions and Updates, then selecting Updates on the left.
It may not work in all cases of seemingly-inexplicable UserControl-incited hangs in the Designer, but it seemed to solve the problem for me. I'm putting this here (rather than deleting the question) in case my experience can help someone else.

How to avoid unnecessary changes in *.designer.cs and *.resx?

For some reason Visual Studio winforms designer time to time applies dull changes to form designer files and respective resx.
Most annoying so far:
changes to order of controls declarations/initialization
changes to some control sizes (most notably menu items widths)
changes to serialized images embedded into resources (...wait, what???)
Those changes doesn't affect form/user control functionality or it look, but they create lot of noise in source control, making merges almost impossible, or require error prone manual fixing to eliminate all changes that actually change noting, just until next change to designer.
Any ideas how to prevent studio from it?
Please try to have your control locked using the right button, then select "Lock controls" on your Form/UserControl.
Unfortunately, there is no way to separate "useful" changes in generated code from "pollution" that come from a control designer bug or internal working.
If the lock control feature does not work for whatever reasons, there is not much that we can do, except those horrible hacks:
Once your form is created and finished, move your generated code (InitializeComponent method and declarations) out of the .designer.cs. Visual Studio will not touch it anymore. The drawback is that you will no longer have visual support.
Having the .designer.cs file flagged as read only will prevent Visual Studio attempting to change it. It might cause Visual Studio control designer hiccups sometimes, but it will leave your .designer.cs intact.
There is another way that consist in not using the designer at all. This leads to a new way to embrace winforms programming, because you will provide the code to initialize controls with their properties and events.
It is not as huge and ugly as it seems to be: using reusable patterns and OO features could be very pleasant, and some well aligned methods calls are better that some horrible code hidden in a .designer.cs file.
Of course there will be no visual support, but you can cheat it by adding your control to an empty container in design mode to see how it looks like.
As far as I understand, you have edited .designer.cs file. From one point it is OK, I guess we all do that. However, there is clearly notification there 'file is auto-generated, do not change it'. So Visual Studio puts you a warning that if you edit it things may not go always well. It is like that by design. And while those auto-changes might not make trouble to your app, they do sometimes (e.g. setting width and height to the control that should change its width and height contextually, and that is always necessary if you have multi-language app).
So if you don't like garbage, you don't use designer, because it creates garbage, that's how it works. Or you start with it and at some point decide to never open it again and then do your cleaning.
Or you go WPF, it meets your requirements, and it is about time to make WinForms legacy, at some point support for it will surely end.

Windows form keeps re-sizing itself in design mode after running debugger

I have a windows form that keeps resizing itself in design mode after running the debugger. Before running my application, I've checked the size of the form in question and everything looks perfect. Then when I run the program, exit, and look at the form in the designer, the form has automatically shrunk itself...in the designer. It isn't a huge difference, but it's enough to irk the ever-living crap out of me as panel borders get cut off when this happens and it just looks bad.
Does anyone have any ideas on what may be causing this? It's not autoscroll, and the form is double buffered with a fixedsingle border. It's also locked, and the size of the form is definitely not larger than my screen. There doesn't seem to be much of a pattern to when this happens. Sometimes when I run my program, it doesn't resize itself, other times it does...but again, it's resizing itself in the designer which I find pretty weird. Would love to hear some thoughts on what I may be overlooking.

WPF Performance Problems with derived TextBox controls

I have a WPF application that renders input forms based on form-configurations saved in a database.
The forms have many controls (100+) and most of these controls are derived from a TextBox-control. On some machines (fast Hardware, Win7 32Bit, also some elder, Windows XP 32Bit), after entering data to a lot of these forms, input performance goes down. Every keystroke gets a delay of some milliseconds and the only solution to resolve this is to close the application and restart it.
My derived control overrides the metadata of the DefaultStyleKeyProperty to set a custom template.
I'm currently reasearching the app in SciTech memory profiler, but maybe someone has already experienced a similar problem with derived TextBoxes and can give me a hint and spare me some more hours/days investigating the problem?
Update
Look also here
It sounds like you may have something stopping the controls on the "used forms" being GCed.
Firstly opening and use as many forms as possible looking at the windows task manager to see if you memory usage is going up – if it is not there is no point looking for memory leeks
Check you are removing all events handlers you forms/controls have put on any long lived objects.
Check that any objects you databind to implement INotifyPropertyChanged, see KB938416
I have in the past had good results using the Red Gate memory profiler.
You don’t need to have controls created that the user can’t see, 100+ controls will have a cost.
Can you use something list a list control in virtual mode, so your TextBox controls are only created when visible.

How to manually reload the Visual Studio designer for WPF

Is there any way to force the WPF designer to reload or refresh, without rebuilding the entire project or solution?
If there's an error, I get an option in the designer view to refresh the designer. But if I don't have an error, how can I get the designer to refresh after I've made a change?
I'm a little late but this is the best solution1 I've found so far: whenever the designer does stupid stuff, I just kill it's process.
Press Ctrl+Shift+Esc
Navigate to the Processes tab.
Kill XDesProc.exe
This also fixes issues for the properties window (like when it gets jammed and you can't type stuff into it).
1 This is a solution for designer issues. Your issues may also be caused by compilation problems, in which case just right click on the solution in the solution explorer, and clean it. The reason behind it is that sometimes the compilation loses synchronicity with the generated files from XAML, and cleaning the solution just deletes those intermediate files; it's like a reset so your compilation can start off with a clean slate.
To do it fast:
Comfortably it's usually the last one if sorted alphabetically.
When it is, it's almost like a ritual for me to quickly pop up the task manager, click any process, press End, Delete, Enter (done), Esc (exit task manager). Instead of restarting VS and waiting for all the loads & unloads, you can do that in 1-2 seconds.
In newer versions of Visual Studio there is an icon on the bottom of the designer to "Disable Project code". If you toggle this off and on it will reload the designer.
You can add this to the Tools menu in Visual Studio.
Once configured, use Tools..XAML Designer Restart:
Alt+T then L
I tried configuring it for Alt+T then X but this clashed with Tools..Choose ToolboX Items.
Update
These days, I prefer to just hit Ctrl+Shift+Esc to bring up the process manager, then X to skip to XDesProc.exe then Delete to kill the rogue process(es).
The Visual Studio designer attempts to keep the rendered view in sync with the XAML. That's the advertised behavior.
The first thing you should check is that there are no errors in the errors window. It may be something as simple as a missing angle bracket.
The second thing to check is whether you have any code (other than your code-behind) which needs to be compiled for the designer to render your XAML correctly. This includes any of your own datatypes that you instantiate in XAML, any custom controls you have written (e.g. MyTextBlock derived from TextBlock), or any classes directly or indirectly in support of design-time data. If so, you need to rebuild your project.
The last thing to check for is possible bugs in the designer. In spite of the advertised behavior, the designer may get out-of-sync due to bugs. In that close, close the XAML window and re-open it. Other tricks that might work are selecting the XAML tab and then the Design tab, or maximizing the XAML pane.
As far as rebuilding your application goes, you don't need to do this as a habit. You only need to recompile it when the above conditions apply. Once they don't apply, you can just edit the XAML. Another way to say this is that if you haven't modified code, you shouldn't need to rebuild (modulo bugs).
I'm not sure, but I think a build will refresh your view in that situation.
There is any event handled in that XAML file, then mostly it will not display the design preview from Visual Studio. If you want to see the design from Visual Studio, try with Command Binding instead of event, you will see the preview.
I'm not sure how this works in WPF editing, but with ASP.NET pages when the design view wont update i can do 2 things
Exit Visual Studio and restart
Go into source view (not split), type something and remove it (not by undoing, just delete or backspare) and save it. Then return to design view, usually the view has been updated then.
When you add a new row of code or a new object, XAML designer is sync but I encountered non-sync behavior when a property of an object is changed.
A tricky way is that when you change a property you only need to remove a ">" character from end of an instruction then retype it.
On the toolbar in the XAML designer, choose the "Disable project code" button to reload the designer link which stays on the right side of "Turn on snapping to snaplines".
Disable project code in the designer
If it is disabled, you can try to check the configuration manager and change processors to "Any CPU".
For projects that target ARM or X64 processors, Visual Studio cannot run project code in the designer, so the Disable project code button is disabled in the designer. Check this:
Debug or disable project code in XAML Designer
For information, I had the same issue with the XAML Designer of Visual Studio Community 2017, i.e. sometimes the designer doesn't show anything, the easiest solution is then to close the XAML file and reopen it.
I also frequently get the exception "An Unhandled Exception has occurred - Click here to reload the designer - Details: The XAML Designer has exited unexpectedly" (the click restarts the designer successfully).
Note that, in this VS version, the process of the XAML designer is not named XDesProc.exe, but UwpSurface.exe. If you prefer or have to kill the process, then the designer shows the same exception as above, and you may restart it.
use process hacker and kill the WpfSurface process (blend only)
Update for designer refresh/reload Visual Studio 2022 Xamarin, taskkill /IM java.exe

Resources