Visual Studio 2010 RC Details

The Visual Studio 2010 launch is currently scheduled for April 12. In the meantime, we saw the release of Visual Studio 2010 RC to MSDN yesterday, with general availability this coming Wednesday. There are many performance-related enhancements and stability fixes to this build.

One item strangely missing from the RC is Silverlight 4 support (although Silverlight 3 is still supported). This is confirmed on Scott Guthrie's blog:
Silverlight 3 projects are supported with today’s VS 2010 RC build – however Silverlight 4 projects are not yet supported.  We will be adding VS 2010 RC support for SL4 with the next public Silverlight 4 drop. If you are doing active Silverlight 4 development today we recommend staying with the VS10 Beta 2 build for now.
Tim Heuer has a post about this as well.

So, follow the advice of Tim and Scott, and continue with Beta 2 if you're doing any Silverlight 4 work.

One thing you might notice, when starting the RC version for the first time, is a long delay during startup. This is due to the construction resource caching. In Beta 2, the caching was constructed as needed, resulting in periodic slowdowns. For more details, see this post in the Visual Studio blog.

The closest I could find to a definitive list of changes between Beta 2 and RC is from Jason Zander, General Manager of the Visual Studio Team. Jason provided this general summary of enhancements on his blog:

  • General UI responsiveness (including painting, menus, remote desktop and VMs)
  • Editing (typing, scrolling, and Intellisense)
  • Designers (Silverlight and WPF in particular)
  • Improved memory usage
  • Debugging (stepping, managed / native interop)
  • Build times
  • Solution/project load
.

PLINQ in .NET 4.0

Continuing the parallel programming discussion, I’d just like to give a brief introduction to PLINQ. Over the last few years, we have not seen a big jump in CPU’s clock speed. What we have seen are chip makers creating multi-CPU chips and there is not an easy way for developers to take advantage of this. One of the areas that would benefit from parallel processing is LINQ. The LINQ syntax is very easy to write and understand but for larger queries the retrieval is too slow. Fortunately in .NET 4.0, Microsoft add extension methods to LINQ so that we may access multiple cores.


Let’s take a look at the syntax and speed differences between a PLINQ and LINQ query. First is our LINQ query:


_sequentialQuery = from b in _babies
where b.Name.Equals(_userQuery.Name, StringComparison.InvariantCultureIgnoreCase) &&
b.State == _userQuery.State &&
b.Year >= YEAR_START && b.Year <= YEAR_END
orderby b.Year
select b;


and the PLINQ query:


_parallelQuery = from b in _babies.AsParallel().WithDegreeOfParallelism(numProcs)
where b.Name.Equals(_userQuery.Name, StringComparison.InvariantCultureIgnoreCase) &&
b.State == _userQuery.State &&
b.Year >= YEAR_START && b.Year <= YEAR_END
orderby b.Year
select b;


So the only difference is the extension method AsParallel().WithDegreeOfParallelism(numProcs) where numProcs is equal to the number of CPU you want to employ for this query.


Now, not all is rosy. There are some caveats to using PLINQ. There is some overhead when these queries run, and for smaller recordsets the PLINQ version may be slower. Below are 2 examples of this.


Here is screenshot of running the above query and returning 3 million records:


PLINQ_3M


We see a big improvement with PLINQ. If we only return 300 records however, we get:


PLINQ_300



So we can see the cost of the overhead. As always, use of PLINQ (or any new technology)requires a lot testing to be sure it is the right method for your current project.

Parallel Programming in .NET 4.0

There is a lot to like in the upcoming release of .NET 4.0 but one thing that recently caught my eye was parallel programming support. In a nutshell, the parallel programming classes allow you to leverage multithreading without having to deal with all the complexity of threads. In my specific case, I was dealing with a collection of objects where I needed to perform a specific operation on each item in the collection. The current code was simply using a For Each loop and calling the method on each object within the body of the loop. I decided to see how big of an improvement I could get by refactoring the code to use the parallel programming classes.

First, the let’s look at the class that was being stored in the collection:

public class MyAccountingMethod
{
     public MyAccountingMethod() { }

     private int _theAnswer;

     public int TheAnswer
     {
         get { return _theAnswer; }
     }

     public void CalculateTheAnswer()
     {
         //Substitute this with actual work
         System.Threading.Thread.Sleep(10);
         Random rnd = new Random();
         _theAnswer = rnd.Next();
     }
}

So, the scenario was that I had a collection of MyAccountingMethod objects and needed to call the CalculateTheAnswer method on each one (the results would be consumed later). You’ll have to use your imagination and replace the body of the method with something more useful.

The first step is to actually generate the collection of MyAccoutningMethod objects. The Enumerable.Repeat method comes in very handy for this:

private List<MyAccountingMethod> GetAccountingList(int count)
{
     return Enumerable.Repeat<MyAccountingMethod>(new MyAccountingMethod(), count).ToList<MyAccountingMethod>();
}

Next, let’s mimic the existing code by using a typical For Each loop:

private void button1_Click(object sender, EventArgs e)
{
     List<MyAccountingMethod> accountingList = GetAccountingList(1000);

     System.Diagnostics.Stopwatch timer = System.Diagnostics.Stopwatch.StartNew();
     foreach (MyAccountingMethod accounting in accountingList)
     {
         accounting.CalculateTheAnswer();
     }
     timer.Stop();

     MessageBox.Show(timer.ElapsedMilliseconds.ToString());
}

Followed by the parallel programming approach:

private void button2_Click(object sender, EventArgs e)
{
           List<MyAccountingMethod> accountingList = GetAccountingList(1000);

           System.Diagnostics.Stopwatch timer = System.Diagnostics.Stopwatch.StartNew();
           System.Threading.Tasks.Parallel.ForEach(accountingList, accounting =>
           {
               accounting.CalculateTheAnswer();
           }
           );
           timer.Stop();
           MessageBox.Show(timer.ElapsedMilliseconds.ToString());
}

Notice the For Each has been swapped out for a call to Paralell.ForEach. The method (which has several overloads) takes two arguments: the IEnumerable collection to iterate over and a delegate to be executed in each iteration of the loop. The code uses a lambda expression to define the delegate. See John’s excellent discussion on delegates and lambdas if the syntax is unfamiliar.

The result? Your mileage may vary depending on how many cores your machine has, but on my test system the processing of the loop went from an average of 10,700 milliseconds using the For Each loop to 3,400 milliseconds using Parallel.ForEach. A fairly significant boost. 

[UPDATE]

As Pablo Gazmuri has correctly pointed out, you still need to make sure that whatever it is you are doing inside the Parallel.ForEach is being done in a thread-safe manner so keep that in mind. Specific to collections, .NET 4.0 introduces a new set of thread safe collections.

Finally, for anyone interested in digging deeper, Patterns for Parallel Programming is a good read.

Feb 10 - Silverlight 4 talk, Rockville, MD

On Wednesday, February 10, I'll be giving a Silverlight 4 talk at the Rockville .NET User Group monthly meeting. The Silverlight 4 beta was introduced at PDC in November and has some really cool new enhancements, including improved out of browser functionality, drag and drop, printing support, and Visual Studio enhancements. I'll also demonstrate some of the latest updates in the Silverlight Toolkit.

If we make it through all of the Silverlight goodies, I'll show how to deploy a Silverlight app to Azure.

The fun starts at 6:30. See you there!

FredNUG User Group: Silverlight 4 Presentation Materials

On January 19th, I presented an introduction to Silverlight 4 and its new user interface enhancements at the Frederick, MD .NET User Group. Here is the source code and PowerPoint from the talk.

Some takeaways we discussed:

Out-of-browser support

Silverlight 4 now offers more capabilities when applications are installed out-of-browser. To enable this feature, look at the Silverlight project’s Properties. You’ll then see an option for enabling out-of-browser, follow
oob-option
After selecting this option, view the out-of-browser settings:
oob-settings
Here, you can customize the shortcut name and window title, as well as a description that pops up when you float the mouse over your desktop shortcut. In our demo app, I also set the window size, based on our MainPage user control.
We saw elevated trust in action with Tim Heuer’s Twitter Example, where a WebClient call was made to the Twitter service without the need for a cross-domain policy, and where network credentials were specified.
To demonstrate out-of-browser within our demo app,  I added another feature, “Notification Window,” to the demo code. This new feature allows you to pop up a notification box in the lower-right region of the screen,  similar to what you see with Microsoft Outlook. In the demo, drop a picture onto the drop target, and you’ll see a window pop up.  If you look at the code-behind, you’ll see two samples for setting up the notification window’s content. Note: I intentionally omitted any code forcing you to install the demo as an out-of-browser app. This way, you can try out the notification window in the browser and observe the exception thrown.

Media support

You now have access to all of your audio and video devices. When I demo’d this, we had two webcams to choose from. . The first time you select a video device, you’ll see the Silverlight warning box. After you agree to allow the app to use the webcam, you won’t see the warning box again (until you restart the application). I added a Stop button, which returns the video rectangle to a green background.

Improved mouse support

Silverlight 4 now has events for right-click actions, as well as mouse scroll wheel support. I demo’d right-click support by adding a popup window when right-clicking the drop-target button.

Drag-n-drop support

You can now drag files from your desktop or file folders to a user interface element. I showed this by setting up a button as a drop-target. This button accepts image files such as png and jpg.

Visual Studio improvements

While this isn’t specifically related to Silverlight 4, Visual Studio 2010 now has a built-in XAML Visualizer. This means you no longer need to open Expression Blend simply to layout your user controls. Blend still has a considerable feature set beyond that of Visual Studio, but for general layout, the built-in visualizer should be fine.

Lots more!

There are many more features in Silverlight 4, such as the new printing capability and COM support. For a more complete list, check out Tim Heuer’s post, where he provides descriptions or samples for each of the new features.
To get started, visit the Silverlight 4 developer page to grab the latest SDK and Silverlight developer runtime. You’ll also need Visual Studio 2010, which is currently in beta (Beta 2 Ultimate is available here).