Professional Visual Studio
Tips, Tricks, and Best Practices for professional .net developers

One of the challenges with debugging applications with multiple threads in Visual Studio is that when you place a breakpoint you tend to interrupt the flow of the application.  This can often mean that tracking down an issue is very time consuming.  Let’s take a very simple example where we have a thread that performs a task then sleeps for 100 milliseconds.

private void ThreadWork()
        {
            for (int i = 0; i < 100000; i++)
            {
                DoWork();
                System.Threading.Thread.Sleep(100);
            }
        }

Now say that something is going wrong on this thread, the first thing we can do is to place a normal breakpoint:

image

Here we can see a regular breakpoint that has been hit and notice that you can hover over variables to see what their current values are. But what happens if the error only happens after say 1000 iterations.  We can use a breakpoint hit count to limit when the breakpoint is hit. Right clicking the line with the breakpoint you can set the Hit Count property via the Breakpoint –> Hit Count… menu item.

image

In this case we want the breakpoint to be hit after 1000 cycles:

image

Note that setting the Hit Count sets up a rule where by the breakpoint will be hit based on the number of times the line has been executed.  If the loop was instead modified to the following the breakpoint would be hit after 1000 * 100 iterations of the for loop, rather than the 1000 iterations we wanted.

for (int i = 0; i < 100000; i++)
           {
               if (i % 100 == 0)
               {
                   DoWork();
               }
               System.Threading.Thread.Sleep(100);
           }

To get around this we can use a breakpoint Condition.  Again this can be set using the Breakpoint –> Condition menu item from the right-click menu.

image

In this case every time the breakpoint line is reached the expression i>=1000 will be evaluated and the result used to determine if the breakpoint has been reached.  Note that this expression can be as complex as you would like but it does add an overhead to execution.  If timing is important then you should make this expression as simple as possible.

If you have specified either a Hit Count or a Condition you will see that the marking alongside your code has changed slightly.  Instead of the normal red dot, you should now see a red dot with a white plus sign inside it.

image

One thing to be aware of is that Hit Count and Conditions are not mutually exclusive which means that you have have both of them set.  As they use the same breakpoint marking it’s not that easy to see if they have both been set.  However if you right-click the breakpoint line you will see that the menu actually indicates which items have been set.

image

Unfortunately the process of actually stopping the execution of your application at a breakpoint often has the side effect of hiding or obscuring the issue you are trying to track down.  Luckily there is a way that you can write an output to the output window and continue execution.  From the right-click menu select the Breakpoint –> When Hit… item.

image

This dialog allows you to either specify an output to be written to the output window or to run a macro.  You can also toggle whether execution continues or breaks when the breakpoint is hit.

In this screen shot the output consists of a number of parts.  Essentially the output is just a string literal that will be printed to the output window.  However, $FUNCTION is a predefined value corresponding to the current function name which will be substituted into the output.  The dialog points out that there are a number of other predefined values that can be used.  Quite often you will want to put your own custom values in there – this is where the { <expression> } syntax is used.  In this case we have chosen just to output the value of the variable i, but we could make this expression more complex so long as it can be converted to a string. Again the more complex this expression is the more it will affect your application.

Now when your breakpoint is encountered by your application it will no longer break, it will simply write a string to the output window.  This can help you track down issues on background threads without affecting your application execution significantly.

Over the last couple of weeks Visual Studio has started to infuriate me.  First it was the WPF designer and just now it was adding new items to the toolbox.  Essentially what was happening was that Visual Studio would just disappear; no errors, no dialogs, no crash report. 

The WPF designer issue crops up whenever I wanted to edit a xaml file.  I’d tried opening it as XAML only but to no avail.  Opening it as just xml works and allows changes but of course you lose all your intellisense (writing XAML is a pita at the best of time and just a miserable experience if you don’t have intellisense).  I half resolved this issue: Karl has a post here that points to a hotfix that works on all platforms except Windows 7 (that would be me). My resolution involved moving to a different machine that wasn’t running Windows 7 when doing WPF work.

Now when I went to add a new item to the toolbox this morning I ran into what appeared to be the same issue.  This connect issue shows a number of people seeing the same issue.  Most of them have found solace in removing PowerCommands, even if they didn’t have it installed in the first place. However, the workaround is actually to run Visual Studio in safe mode (ie devenv /safemode) as discussed here and here

That’s right it’s time to start looking into the future at what’s coming down the line with the next iteration of both Visual Studio 2010 and the .NET Framework 4. Luckily, there is an awesome Channel 9 show called 10-4 that is covering a range of topics.  These are based on the currently available CTP and provides a great starting point for getting familiar with what’s in the next release of these products.

So far, here’s a rough list of episodes:

10-4 Episode 1: Working with the Visual Studio 2010 CTP VPC

10-4 Episode 2: Welcome to Visual Studio 2010

10-4 Episode 3: ASP.NET WebForms 4.0

10-4 Episode 4: No More Parallel Development Pain

10-4 Episode 5: Code Focused in Visual Studio 2010

10-4 Episode 6: Parallel Extensions

10-4 Episode 7: No More Planning Black Box

10-4 Episode 8: Pure Client-Side Development with ASP.NET AJAX 4.0

10-4 Episode 9: Visual Basic 10

10-4 Episode 10: Making Web Deployment Easier

10-4 Episode 11: Bi-Directional Routing with ASP.NET WebForms 4.0

10-4 Episode 12: Simplifying Your Code With C# 4.0

[Updates]

10-4 Episode 13: No More Late Surprises

10-4 Episode 14: Sentient DSLs

10-4 Episode 15: Model-First Development with the Entity Framework 4.0

10-4 Episode 16: Windows Workflow 4

Today I spent some time repaving my computer with the PDC build of Windows 7.  Whilst it’s probably a long way out I thought I would see just how far Microsoft is along with the next version of Windows.  After all, the reports I’ve been hearing have been very positive about it and to be honest, it can’t be much worse than Vista.

I encountered a couple of minor issues early on but nothing that was insurmountable.  When it came to installing Visual Studio 2008 the core product installed fine but SP1 kept on failing – I tried a couple of times thinking it was caused by me doing other things whilst the install was progressing. 

I took a quick look at the error log and noticed it failing on one assembly to do with Visual Studio Tools for Office. 

image

Drilling into the VSTO specific log I was able to pull out the knowledge base number, ie KB949258.

MSI (s) (F0:5C) [18:27:43:876]: Windows Installer installed an update. Product Name: Visual Studio Tools for the Office system 3.0 Runtime. Product Version: 9.0.30729. Product Language: 0. Manufacturer: Microsoft Corporation. Update Name: KB949258. Installation success or error status: 1603. 

This meant I was able to look up the individual update – Microsoft Visual Studio Tools for the Microsoft Office System (version 3.0 Runtime) Service Pack 1 (x86).  Downloading and installing this before SP1 of Visual Studio 2008 enabled the installation to progress nicely.

Do you want to integrate Virtual Earth into your website but are daunted by the prospect of using the javascript object?  Well as part of the Windows Live Tools for Visual Studio there is an ASP.NET Virtual Earth control that you can drag onto your aspx page, set properties add event handlers, all without writing a line of javascript.

To get started you will of course need to download and install the Windows Live Tools for Visual Studio.  The November CTP is currently available for download.

Once you have installed the tools you will notice that there are some additional toolbox items in a tab entitled Virtual Earth:

image

Select the Map and drag it onto your aspx page.  Note that you will also need a ScriptManager control as the Virtual Earth ASP.NET control uses this to communicate with the server so that you can wire up events.

image

As you can see you can set properties on this control such as the Zoom level, whether Traffic information is displayed and even whether the scale is in Miles or Kms.  What’s even better about this control is if you select the Event tab in the properties window you will see that there is a list of server side events that you can wire up.

image

Here you can see I’ve added an event handler for the ServerClick event.  The Server prefix is there to indicate that the event is raised server side. Here is some code that adds a polygon shape to the map when the map is clicked.

using System.Collections.Generic;
using Microsoft.Live.ServerControls.VE;

namespace VESampleApp
{
    public partial class _Default : System.Web.UI.Page
    {

        protected void Map1_ServerClick(object sender,
                                        MapEventArgs e)
        {
            Shape s = new Shape(ShapeType.Polygon, new List<LatLongWithAltitude>(){
                                                        new LatLongWithAltitude(20,20),
                                                        new LatLongWithAltitude(20,25),
                                                        new LatLongWithAltitude(25,25),
                                                        new LatLongWithAltitude(25,20)
            });
            s.HideIcon();
            this.Map1.AddShape(s);
        }
    }
}

Kevin Hoffman has a very quick overview of getting started with the Live Framework SDK and Tools for Visual Studio. I just wanted to go through the process in a little more detail to point some things out along the way.  The first thing you will need to do is to get yourself signed up to the Live Framework CTP.  This can be done via the Microsoft Connect website – there currently seems to be a waiting list for this which Microsoft will hopefully open up as they get closer to release for the Live Framework.

Once you are on the CTP program you need to install the following components:

You will also need to complete the registration process by going to the Azure Services Developer Portal.  If you don’t already have an Azure account you may be prompted to agree to the services terms and conditions after signing in with your Live Id.  Once in, you will need to go to the Account tab where you can reclaim the token you would have been assigned by entering the CTP program – this will enable you to create Live Framework projects.

Now that you are ready, let’s begin by creating your first Silverlight Mesh-enabled Web Application.  You can do this by opening Visual Studio 2008 and selecting File>>New>>Project.  In the New Project dialog you will see that there is now a tree item for Live Framework where there are some templates for creating Mesh applications. 

image

Here we are selecting the Silverlight application and of course we are doing the canonical Hello [World] Mesh application. Upon selecting Ok you will see that two projects are created.  One is the actual Silverlight application, whilst the other is used for packaging and deploying to the Azure platform ready for debugging – yes, that’s right you can debug your application whilst it is running in the cloud.

image

Here we have only slightly modified the text in the created TextBlock but other than that there have been no other changes.  Without doing anything further we can go ahead and hit F5 to begin debugging the application in the cloud.  When you do this you will notice the following dialog appear.

image

These three steps will take a couple of minutes but luckily only need to be done the first time you are debugging your application.

1.You will need to go back to the Azure Services Developer Portal (this can be done by clicking the link).  There you will need to hit the New Project link.

image

Then select the Live Framework CTP project type.  If this is disabled then you haven’t got a valid token registered against your Live Id account.

image

Next, fill in the details about your project

image 

Select Mesh-enabled Web application, and hit Create to complete the project creation wizard.

image

2. Now, return to the dialog in Visual  Studio and hit the “Copy full path of HelloSilverlightMesh.zip to clipboard” link.  This is the local path for the compressed file that you need to upload to the cloud that represents your application.

Return to the Azure Services Developer Portal where you should still have the project you just created visible.

image

Select the Upload Package button, then hit the browse button.  When prompted for a file to upload, just paste (Ctrl-V) the path from the clipboard and press Ok.

image 

With a file select, hit the Deploy button to upload the application to the cloud.

image

I’ve experienced some issues at this point where it hasn’t accepted the application the first time I’ve attempted to upload it.  You may need to retry this step a couple of times. Once it has been done successfully you will notice that the project information page has been updated to include additional information.  This includes the Application Self Link which is required in the third step.

image

Select the Application Self Link url and copy (Ctrl-C) into the clipboard.

3.Paste the url into the textbox in the Visual Studio dialog

image 

Click Ok to proceed with application deployment.

Once deployment has been completed you will notice that a new Internet Explorer window appears with your Live Desktop (you may be prompted to Sign In).  Open on your Live Desktop should be your newly created application.

image

Notice that there is an application icon on the desktop so that you can restart your application at a later stage.

As Kevin points out, where this becomes really cool is that this application can be synchronized to your desktop.  To do this you unfortunately need to uninstall the publically available Live Mesh client.  Once you have done this, go to the Mesh CTP Live Desktop (https://developer.mesh-ctp.com/) and click on Add Device – from here you can download the LiveFrameworkClient installer.  This is essentially Live Mesh but is only enabled for application synchronisation.

After installing the Live Framework Client you will of course be prompted to sign in. As with the Live Mesh Client you will have to enrol your computer into the (developer) mesh, which will in turn synchronise the Silverlight Hello Mesh application to your desktop.

image

Double-clicking this icon, like any other icon, opens the application.  Despite being a Silverlight application it runs outside the browser with the familiar Live Mesh information tab alongside it.

image

This is the start of interesting times for application developers.  I think that the Live Framework and other services offered by the Azure Platform will significantly change the way we architect applications into the future.

The October edition of the Win a copy of Professional Visual Studio 2008 competition has now closed and our winners have been drawn. I am pleased to announce the winners for this month are Tomasz Smykowski from Poland and Simon Andersson from Sweden. Congratulations to you both.

Everyone else has another chance of winning one of the 6 remaining books starting right now. To enter the competition you need to either:

  • Write a comment on any post (it doesn’t have to be this one); or
  • Link to this blog from your own website/blog (We accept trackbacks, or you can let us know about the link via email).

At the end of November we will draw two new winners at random from those who participated during this month.

The competition is open to everyone. If you are lucky enough to be drawn as a winner, we will contact you via email before announcing the winners. Winners will need to provide us with their name, telephone number, and shipping address.

This competition has been made possible due to the generosity of our publisher, Wiley Publishing.

Good luck!

We’re very pleased to make announce that you can now download a free chapter from our book, Professional Visual Studio 2008. Based on popular request, we have decided to release Chapter 32: ASP.NET Web Applications. This is one of my favourite chapters from the book and, at 42 pages long, it also happens to be the largest.

Download the free sample chapter [PDF 2.6Mb]

Web Application vs Web Site Projects Designing Web Forms

This free chapter covers the following:

  • Web Application vs. Web Site Projects
  • Creating Web Projects (Both Web Site and Web Application projects)
  • Designing Web Forms (The HTML Designer, CSS Tools, Validation Tools)
  • Web Controls (Navigation, User Authentication, Data Components, Web Parts)
  • Master Pages (Including Nested Master Pages)
  • Rich Client-Side Development (JavaScript, ASP.NET AJAX, AJAX Control Extenders)
  • ASP.NET Web Site Administration (Security, Application Settings)

Also don’t forget that today is the last day to enter our monthly competition for your chance to win your very own copy of Professional Visual Studio 2008.

I just noticed that the first CTP is now available for download.

The September edition of the Win a copy of Professional Visual Studio 2008 competition has now closed and our winners have been drawn. So without further ado, I am pleased to announce the winners are Jay Parzych from Connecticut and Harvey Green from Western Australia. Congratulations to you both.

As for everyone else who entered, don’t despair, the October edition of the competition begins right now, so you still have a chance of winning one of the 8 remaining books.

As with last month, to be in the running you need to either:

  • Write a comment on any post (it doesn’t have to be this one); or
  • Link to this blog from your own website/blog (We accept trackbacks, or you can let us know about the link via email).

At the end of October we will draw two new winners at random from those who participated during this month.

This competition is open to everyone. If you are lucky enough to be drawn as a winner, we will contact you via email before announcing the winners. Winners will need to provide us with their name, telephone number, and shipping address.

This competition has been made possible due to the generosity of our publisher, Wiley Publishing.

Good luck!

« Previous PageNext Page »


About this site

Professional Visual Studio aims to provide tips and tricks, traps to avoid, and industry best practices from experienced .NET developers on using Visual Studio in the most effective way possible.

Copyright © 2007-2010 David Gardner, Keyvan Nayyeri, and Nick Randolph. All rights reserved.
Blog | Archives | Books | About | Contact