How do I clear Expression Blend 4 cached solution information? - expression-blend

I seem to be adept at making Expression Blend 4 crash at startup. The problem seems to be related to a deadlock initializing static managed and unmanaged variables as Blend first opens my solultion which has a mixture of unmanaged C++ and managed code projects. Blend crashes instantiating my ViewModel (I'm using MVVM Light in App.xaml, but I would think this isn't really relevant). This page describes in detail how to detect and correct this potential deadlock: Initialization of Mixed Assemblies
Since I'm writing code and markup in both VS2010 and Blend4 simultaneously, I sometimes accidentally create this situation, and thereafter Blend crashes when loading my solution. The Blend startup crash persists even if I correct the issue in VS2010 (VS2010 seems immune to crashing on the same solution file which brings down Blend). The Blend startup crash can happen on both x86 and x64 systems. The Blend startup crash persists even if I try to delete all output directories. The Blend startup crash persists even if I rename "C:\Users\username\AppData\Local\Microsoft\Expression\Blend".
My question is: How do I reset Blend to a state as though it has never seen this solution before?
I'm assuming this would be a valid workaround, since if I download a new, fresh copy of my source code from source control with the managed/unmanaged problem fixed, it loads in Blend4, builds, and runs just fine.

The main reason why Blend crashes is because when Intialising UI components it actually runs their constructor which can have some code for example accesses database and Blend doesn't support DB access so it might crash.
So there are two ways to find a potential problem.
Comments out all the code in the constructor of your UI ellements apart from InitializeComponent() to find out problematic code
Or
Attach Visual Studio Debugger to Blend and then rebuild the project or open XAML file which craheshes blend
To do 2nd option you would go to Debugger -> attach to process -> pick Blend from the list.
When you identify the code which crashes Expression Blend just have an if statement which will stop running the code if it's the blend who tries to execute it and if it is not just run it. So something like this:
if(DesignerProperties.IsInDesignModeProperty)
{
// This code will run when Blend renders the controls
}
else
{
// This code will run when you are running application on it's own
}
Hope this helps.

Related

Visual Studio solution explorer incorrectly identifies classes as custom controls

I have a project in Visual Studio which is misidentifying some of the files as 'user controls' as opposed to code-only classes. The project compiles and runs just fine, but it does leave a slightly annoying issue that when these files are double-clicked, the designer window is presented instead of the code editor.
Obviously, there are plenty of ways to get to the editor but, firstly it's annoying, and secondly, I don't know if this could lead to other issues later down the road so I'd like to resolve the problem sooner rather than later.
As you can see from the screenshot, there are 5 files, all of which are code-only classes, yet 4 of them are identified as custom-controls by the solutions explorer; only the final one is identified correctly.

What could cause lag using Visual Studio 2010 with a Direct3D control?

I've got a really pesky problem. When writing a C# application using the .NET 4.0 framework and Direct3d for managed code, and creating a device inside a user control (so that I can use it as a kind of viewport), my visual studio 2010 starts "lagging" after the first compile of it. When I type something, there is a delay about 0.5s - 1s until the typed text is shown. The same delay occurs while scrolling via page down or page up or arrow keys. With other projects which are even bigger - with up to 1000 source files (C++), or other C# projects - there is no problem like this. Mouse interaction also delays for 0.5s - 1s.
So my question is what could cause this problem and how can I avoid it?
EDIT: One more hint is that I often get by compiling it:
Unable to copy file "obj\x86\Debug\Direct3DTest.exe" to "bin\Debug\Direct3DTest.exe". The process cannot access the file 'bin\Debug\Direct3DTest.exe' because it is being used by another process.
You probably have your UserControl in design view opened either in it's own document or embedded in your form. This probably causes a DirectX device to be created inside VS. Check fi that's the case and if so you should be able to find out if your control is in design mode (in VS) or in runtime mode.

Visual Studio designer stops recognising changes occasionally

Using VS2008 visual designer occasionally the designer seems to stop recognising changes. Normally you get a * next to the filename in the tabs when a change is made but sometimes this stops appearing when I am making changes. When this happens may changes are getting "lost" so if I close/reopen the file all my changes are gone.
Has anyone else encountered this and know why it happens and if there's a fix/workaround?
Cheers.
We ran into a similar problem in Visual Studio 2010. When attempting to resize a user control, the designer simply refused to recognize that a change was made (no * shown), and when you rebuilt, all source file changes were lost.
I found this link from another person having the same problem.
After stepping back through source control and comparing the .designer.cs file for the user control and its child controls, it turned out to be the same problem. At some point, the line:
this.components = new System.ComponentModel.Container();
disappeared from the designer file of one of the child controls (not the user control that I was having problems with). Incidentally, within that designer file, all of the references to "this.components" were also removed.
After fixing the child control (restoring the creation of this.components, and fixing the original references), the top level user control then started recognizing changes again.
We're not sure what caused the corruption of the child control. Perhaps it was a source code merge or a designer bug.
It's a very difficult problem to debug. Even if you attach a debugger to debug the design time behavior of the user control (and its children), there are no exceptions thrown and there is no indication that anything is wrong.

VS 2008 Designer not executing default constructor anymore?

I just tested which type of code is executed by the WinForms Designer in VS 2008.
It seems that the designer is not executing the default constructor anymore.
I added a simple thing like this.Text = "foo"; nothing is changed in the designer.
Did I miss a change between VS 2005/2008? (or SP1)
tia
Did you set a breakpoint to see it skip?
If its the first form in the project, instead of starting with "Run", you can F10 (Step Over) to start at the first line of code. From there you can trace the execution.
.NET 2.0 and above use Partial Classes for windows and web forms. Is it possible that the constructor that is being hit is not default.
I placed several statements (setting the background, MessageBox.Show, File.WriteAllText,...) into the constructor including System.Diagnostics.Debugger.Break(), but nothing causes any type of response...
Not to misunderstand me, it is the behavior I'd like to have, but I know I had problems in VS 2005 regarding this issue, and I don't find any info about the fact that there is a change in the Winforms Designer.
Using the .NET reflector I don't see any additional constructors, but maybe the designer is creating one on-demand, ...

Good Way to Debug Visual Studio Designer Errors

Is there a good way to debug errors in the Visual Studio Designer?
In our project we have tons of UserControls and many complex forms. For the complex ones, the Designer often throws various exceptions which doesn't help much, and I was wondering if there's some nice way to figure out what has gone wrong.
The language is C#, and we're using Visual Studio 2005.
I've been able to debug some control designer issues by running a second instance of VS, then from your first VS instance do a "Debug -> Attach to Process" and pick "devenv".
The first VS instance is where you'll set your breakpoints. Use the second instance to load up the designer to cause the "designer" code to run.
See Debugging Design-Time Controls (MSDN).
It has been a pain in 2005 and still is in 2015. Breakpoints will often not hit, probably because of the assemblies being shadow copied or something by the designer(?). The best you can do is to break manually by introducing a call to Debugger.Break(). You may wrap it into a compiler conditional as so:
#if DEBUG
System.Diagnostics.Debugger.Break();
#endif
int line_to = break; // <- if a simple breakpoint here does not suffice
I have had this happen many times and it is a real pain.
Firstly I'd suggest attempting to follow the stack trace provided by the designer, though I found that often simply lists a bunch of internals stuff that isn't much use.
If that doesn't work then try compiling and determining the exception from there. You really are flying blind which is the problem. You could then try simply running the code and seeing what exception is raised when you run it, that should give you some more information.
A last-gasp approach could be to remove all the non-generated code from the form and gradually re-introduce it to determine the error.
If you're using custom controls you could manually remove the generated code related to the custom controls as well if the previous method still results in an error. You could then re-introduce this step-by-step in the same way to determine which custom control is causing the problem, then go and debug that separately.
Basically as far as I can tell there's no real way around the problem other than to slog it out a bit!
I discovered why sometimes breakpoints are not hit. In the Attach to Process dialog, "Attach to:" type has to be "Select..."'d.
Once I changed to "Managed 4.0, 4.5", breakpoints for a WinRT application were hit. Source: Designer Debugging in WinRT.
Each one is different and they can sometimes be obscure. As a first step, I would do the following:
Use source control and save often. When a designer error occurs, get a list of all changes to the affected controls that have occurred recently and test each one until you find the culprit
Be sure to check out the initialization routines of the controls involved. Very often these errors will occur because of some error or bad dependency that is called through the default constructor for a control (an error that may only manifest itself in VS)
You can run a second instance of VS and attach it to the first instance of VS (Ctrl+Alt+P). In the first instance set the breakpoints, in the second instance run the designer, and the breakpoint will fire. You can step through the code, but Edit-and-Continue will not work.
For Edit-and-Continue to work, set you control library's debug options to run a VS with the command line argument being the solution filename. Then you can simply set the breakpoints and hit F5. It will debug just like user code! As a side note, you can do this will VS and Office add-ins also.
This worked for me for Visual Studio 2022:
I opened a second Visual Studio instance
In the second instance I clicked Debug -> Attach to Process...
I selected DesignToolsServer from the process list
More details: https://learn.microsoft.com/en-us/dotnet/desktop/winforms/controls/walkthrough-debugging-custom-windows-forms-controls-at-design-time?view=netframeworkdesktop-4.8

Resources