Wintellect  

 

By now you've probably heard of the steady march toward multiple CPUs in your computer. First there were two core CPU's and then a few months later there was four core ones available. The number keeps growing, with Intel predicting 80 core within a few years.

Naturally the development world looked at multiple cores and started thinking about how to write parallel algorithms. But some developers took a side road to completely different parallel programming world. These developers realized that there was another processing unit on most computers that had vastly more parallel processing power. I'm talking about your computers graphic processor unit (GPU).

Modern GPUs are designed to apply parallel algorithms to pixels. The pixel information is just byte data so some smart people figured that the GPU might be good at certain non-graphic algorithms. It turns out they were right. There are problems that lend themselves to running on the GPU. These include financial, bioscience and cryptography problems.

NVIDIA is a big player in the GPU world and they took notice of this new development. And they decided to enhance the market by creating specialized development tools. Here's what I find interesting; NVIDIA decided to build a Visual Studio extension to permit multi-threaded, parallel debugging for these new GPU applications.

In July 2010 NVIDIA released a free edition of a tool called Parallel Nsight. http://developer.nvidia.com/object/nsight.html

Quote from NVIDIA site

What could you do with your application running 5 – 50 times faster? Develop an innovative approach to cancer detection? Analyze financial options in real-time? Discover hidden oil reserves? Create award winning game physics? Process HD video to allow your customers to meet deadlines? Our partners are doing all of the above, and more.
The power of GPU Computing is already delivering game changing performance increases to the Medical, Finance, Energy, Consumer and Research fields. For over 1,000 customer stories, visit www.nvidia.com/cuda.

This tool integrates within Visual Studio 2008 SP1. According to NVIDIA they will have a working version for Visual Studio 2010 by the end of the year.

Nsight includes approximately ten tools that simplify debugging, profiling an integrated build tools. It fully exploits the Visual Studio debugging Windows.

I still find it fascinating what we can do with computers.

Once or twice a year Wintellect offers my Windows Presentation Foundation (WPF) class as a two day virtual online class. it’s a busy two days, I cram a lot information about WPF basics into a short amount of time. it is always hard for me to decide what to include in these two days; you see I have about eight days worth of the material that I’ve written over the last couple years. Yes WPF is a big framework.  Anyway I put together a good two days worth of topics and the class is always a lot of fun and filled with great people asking interesting questions.

We are offering the class again this summer on August 30th.  Looks like we still have some seats available for the class so please join me for the two days.   I promise you’ll learn a lot about WPF and have a good time in the process.

Sign up here.

https://www.wintellect.com/Registration/WintellectRegistration.aspx

This week I was debugging a problem with a WPF application.  Actually, it being a WPF app had little to do with the problem.  No, what I was testing was the installer process.

 

We’re writing a new WPF frontend for an existing system and the application needs to  installed via a InstallShield installer.  It’s a legacy system and has dependencies on older systems and software.  For example, during the setup we install a Access 2003 mdb file into the %appdata% folder.    The installer is also supposed to install the prerequisite technologies, like .NET 4.0 and copy a files to numerous locations on the local drive.  Then on application start we need to dynamically find these files. Trouble was, when the app started for the first time, it immediately crashed.  

Looking through event log revealed a unhandled exception as the cause of the crash.  The app has an event logger, but the code didn’t seem to be running.  Checking the installer logs didn’t show any obvious problems either.  I decided it was time to turn to Visual Studio remote debugging for help.

Debugging on VPC

As you might have surmised by now, I’m testing the install on a virtual PC.  It’s configured with undo disks, so I can roll back the install after each try. After thinking about it for a few minutes I decided put a breakpoint in the apps Main method.  This is trivial in most circumstances, but I wasn’t sure how to accomplish this in my scenario.  I didn’t want to install VS or other debuggers on the VPC.  This seems like a prime use case for Visual Studio 2010 remote debugging.

I won’t bore you with the steps to configure you machines to enable Remote Debugging. After about 15 minutes work I had the remote debugging working. John Robbins has a nice post about setting up Remote Debugging [1] on a local Virtual PC if you want to create a similar setup. 

How to hit breakpoint in Main method

Normally the way you remote debug it to start the msvsmon.exe on the VPC (be sure and run as administrator), then launch the app.  Once the app is running, you return to Visual Studio and choose Debug\Attach to Process, then pick your instance of msvsmon and choose the remote process.

image 

But thing are a little tricky here. Do you see the problem with that approach?  Yes, by the time you get the app running and return to Visual Studio your program is long past executing the code in the Main method and other startup methods.

 

My solution, put a 30 second Thread.Sleep in the Main method.  That gave me time to switch back to the other screen and get the debugger attached.

  App app = new App();
  Thread.Sleep(1000 * 30);
.. breakpoint here app.MainWindow = new MainWindow(); app.MainWindow.Show();
  app.Run();

It worked perfectly, I was able to trace the  problem to a typo in the installer.  I’m sure there are approaches to take however.  Do you have a favorite technique?

[1] http://www.wintellect.com/CS/blogs/jrobbins/archive/2010/06/15/vs-remote-debugging-across-workgroups-or-domains.aspx

Tuesday morning started out good.  I was in a meeting room on the Microsoft campus, working on a Windows Phone 7 (wp7) application.  About 10:A.M. my local developer evangelist came to the room.  In his hand was a white and yellow box that contained my new Windows Phone.  Yes, I’m talking about a wp7 device made by LG.  The GW 910 is a phone that contains a slide out keyboard.

image

I’ve had the phone for three days now.  First impressions are good, the start screen was intuitive and easy to use.  I toured the included applications.  There aren’t that many, but the core ones look solid.  It’s a novelty to open Microsoft Excel and see the tiny spreadsheet.  Pinch gestures work, so it is easy to scale the cells up to a readable size.

It took me a while to learn how to uninstall applications.  On the wp7 you press and hold the application icon.  After a one second wait a menu pops up which contains the uninstall menu. 

There are still pending changes coming for the main UI.  Those changes are still cloaked however, you’ll need to wait a few weeks to see what’s new.

I didn’t like the keyboard, I prefer the one on my HTC Touch Pro 2.  One the other hand, the onscreen keyboard (known as the Soft Input Panel -SIP) was fast and easy to use, once I got use to the correct way to hold my finger in relation to the virtual keys.

Another hardware #fail, the touch screen is not sensitive enough.  I find myself touching the same icon/button repeatedly with no results.  This is definitely a LG issue.  I spent some time with the Samsung device on Wednesday and its screen was smooth and responsive. Oh, and the Samsung was noticeably faster too.

Writing and deploying apps to the phone

If you are a Silverlight developer you are going to love programming for this phone.   Silverlight just works. Most of the time your code can be ported directly to the phone.  It took me less than than ten minutes to port a Bing Map demo to the phone. The only quirks are understanding what Silverlight version is on the phone. It is part Silverlight 3, some of Silverlight 4 and some extra APIs thrown in for good measure.

Setting up your computer to use the deploy to the phone takes a few steps.  First, you must install all the wp7 tools and Beta SDK.  (learn more at http://developer.windowsphone.com/)  Then you have to install the Zune Beta (aka Dorado) on your local computer.  Once that is done, you just connect a USB cable,make sure the Zune software is running on local computer, choose the device from the drop down in Visual Studio and press F5 to debug.  It’s a beautiful experience.

image

Do software crises ever happen when you have plenty of free time to troubleshoot them?  I’m sure that they do,  it just doesn’t seem that way for me in the last few years.

 

Expression Blend Warning

The first sign of trouble appeared as a gentle nag screen in Expression Blend 4.  “Only 2 days left to activate your license” it whispered to me one morning.

 

“What the heck?” I thought to myself.  I knew that I had activated the license months ago.  Just to be sure, I looked it up.  Yes, about a week after I installed Expression Blend I had requested a license key from MSDN and activated my copy.

 

Re-Activate

So I found my copy of the key and reactivated the license.   This is where it’s starts to get strange.

Side Note: Some of the text is cut-off in these screen shots.  This is _another_ issue with Blend.   I’m running my computer in 115% DPI settings.  Apparently non default DPI settings still cause problems like this.  I thought the Device Independent Pixel framework in WPF took care of this.  I guess not.

 

 

image

Starting the activation process.

 

image

First error during the activation.

 

image

Blend offers to retry.  I try again.

 

image

Good news, or so I think.  Activation success.

 

image

Hmm. that’s not right.  I just activated the software.  Apparently the success message is wrong and I do not have a valid license yet.

Blend team members help troubleshoot

I contacted the Blend team and they put some of their licensing developers on the problem.   I ran some license checking software and sent the results to them to analyze.  And they found the problem.

 

Here’s the question from Chris that put me on the right track.

“Have you made any changes to your machine??  (Replaced a DVD/CD drive or network card??). “

Hmm. now that you mention it I have.  Dell replaced my motherboard a couple weeks ago.  Yesterday, I replaced my hard drive.  And yesterday is when the problem started.

Back from Chris comes this simple sentence .“The license software sees the change in hardware as an attempt to get around the software licensing.”

Bingo, now we know the problem.

Changing hardware may void your license

Expression Blend is not the first product from Microsoft to use this type of licensing policy.   What’s surprising is that all my other Microsoft software on this same computer is happily working without the slightest hint of trouble.

So, what is the resolution you ask?  I need to get a new key from MSDN.  Unfortunately, my subscription only grants me three activations and apparently I’ve already used them all (VPC installs?).  Time to call Microsoft support and plead my case.

Are you a WPF history buff? If so you might be familiar with the attached behaviors story. It’s a short saga, but worthy of review. Back in the early days of WPF we were learning the nuances of the new XAML system. The attached property system was an interesting subsystem of WPF, revealing a clever way to add a property to an unrelated object. A few of the XAML pioneers decide to extend this idea to behavior. Their idea was to define a behavior (code) in a class, register it with the Dependency System and then ‘attach’ the behavior to other classes.

The idea had merit so the Expression Blend team brought the concept into Blend 3. They created a sub-classable type called System.Windows.Interactivity.Behavior<UIElement>, which makes it simple to create custom attached behaviors.

I’m constantly thinking of ideas for new behaviors, here’s one for moving an object to the front of the other items in a Panel.

 

public class BringToFrontBehavior : Behavior<Panel>
  {
    protected override void OnAttached()
    {
      base.OnAttached();
      AssociatedObject.Loaded += 
new RoutedEventHandler(AssociatedObject_Loaded); } void AssociatedObject_Loaded(object sender, RoutedEventArgs e) { foreach (var child in AssociatedObject.Children) { child.MouseLeftButtonUp += new MouseButtonEventHandler(child_MouseLeftButtonUp); } } void child_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { BringToFront(sender); } // call this method to move element to front of ZIndex private void BringToFront(object sender) { Panel parent = (Panel)((FrameworkElement)sender).Parent; int z = GetHighestValue(parent); for (int i = 0; i < parent.Children.Count; i++) { if (sender.Equals(parent.Children[i])) { parent.Children[i].SetValue(Canvas.ZIndexProperty, z); } } } private int GetHighestValue(Panel parent) { int highest = 0; foreach (var child in parent.Children) { var val = (int)child.GetValue(Canvas.ZIndexProperty); if (val > highest) { highest = val; } child.SetValue(Canvas.ZIndexProperty, val - 1); } return highest; } }

I work on an Open Source project called Shazzam Shader Editor.    This week, we released version 1.3.

With this utility you can write, compile and test WPF/Silverlight shaders.

image

You can learn more about this free tool at http://blog.shazzam-tool.com.

Learn more about the features available in Shazzam 1.3.

Download your own copy at http://shazzam-tool.com

A fair portion of the time, the content of a FrameworkElement is text. Some elements, like TextBox and TextBlock make this obvious, by providing a Text property. The Text Property is of type String and it trivially easy to set in XAML.

[example]

  <TextBlock Text='This control is intended to show text' />

Other FrameworkElements are more flexible regarding their content. Take the ContentControls (CheckBox, Label, Button) for example. They expose the Content Property, not a Text property. Content, in this case, means a single child element. This child could be a single TextBlock or a complex nested hierarchy of children items.

[example]

    <Label Content='This control can contain text' />
    <Label>
      <Label.Content>
        <StackPanel Orientation='Horizontal'>
              
        <Image Source='geese.jpg' Width='100'/>
          <TextBlock Text='Or it can contain more complex UI' />
        </StackPanel>
        </Label.Content>
    </Label>
      <!--<TextBlock Content="This will not parse" />-->

WinForm developers are often surprised to learn the Label can work with complex content. The key thing to remember about Label in WPF it that its main purpose is NOT to show text. Rather, its prime mission is to enable 'accelerator-keys' for other controls in the XAML tree. For example, if you want to permit ALT-F to move your input cursor to the First Name TextBox. To do this you place a Label and a TextBox in your XAML. Use a binding to link the two together.

[example]
<Label Target='{Binding ElementName=firstNameTextBox}' Content='_First Name'/> <TextBox x:Name='firstNameTextBox' />

Because the Label is a Content control you can show icons, bullets or other interesting UI in the Label.

Let's say that you want to draw a line in WPF. You'd probably guess that there is a Line class in WPF that does the trick. You'd be right, as it turns out, there is such a class available in WPF. To use it you designate a start and end point, provide a color and line thickness and you get your desired line.

Were you aware that WPF includes another line class? It's called the Polyline and the primary difference is that you provide a collection of Points, typed as a PointCollection, to the Polyline and it draws a line through each point.

Here's how you do that in XAML.

image

image

You can also add points in your application code like this example.

image

image 

Changing the StrokeLineJoin property

You can control the shape of the Polyline where it changes direction with the StrokLineJoin property. Choose from the three values; Round, Bevel and Miter.

image 

image

Dynamically adding Points

If you want to allow your users to draw a poly line on screen it easy to accomplish with the following code.

image

image

 

Creating a dynamic line chart

This last example shows show to create a dynamic line chart. The interesting idea in this chart example is how it updates. When the timer fires a new Point is added to the Points collection. Then the first point in the collection is removed. Next, each point in the point collection has its X coordinate reduced by a constant amount. The effect is that the chart appears to move constantly to the left.

image

Source Code

https://code.msdn.microsoft.com/Release/ProjectReleases.aspx?ProjectName=WintellectPolyline&ReleaseId=4601

I spend a lot of time writing articles, blog posts and code.   Most of the blog posts I write are published on my wpfwonderland blog site.  But I hear from the students of my Wintellect classes that they’d like to see some posts here on the Wintellect website.  So I’ve decide to write more items for my Wintellect blog during the next two months.

Well, there is nothing more boring that posts about blogging however.  So I’ll quit talking now and go write a post about WPF.

Data is at the heart of virtually any business application.  WPF ships with a flexible data binding engine that simplifies writing data centric applications.  I spend a lot of time during my WPF class talking about this data-binding feature.  In fact I devote three sections during the week to this topic.

image

Last week I added a new binding demo to the Data Templates chapter.  The idea is to bind a <MenuItem > header property to the contents of an XML file.  Changing the XML file contents results in updates to the menu structure.

 

<XmlDataProvider x:Key="menuKey"
                   Source="../menu.xml"
                   XPath="menus" />
 
  <Style x:Key="itemStyle">
    <Setter Property="MenuItem.Header" Value="{Binding XPath=@header}"/>
  </Style>
  <Style x:Key="categoryStyle">
    <Setter Property="MenuItem.Header" Value="{Binding XPath=@categoryName}"/>
    <Setter Property="MenuItem.ItemsSource" Value="{Binding XPath=menu}"/>
    <Setter Property="MenuItem.ItemContainerStyle" Value="{StaticResource itemStyle}"/>
  </Style>

You can see this demo and many other data demos during my 2 day WPF virtual class  on March 23-24.

The last couple months have been a hectic flurry of activity. Not only have I been at several conferences and the MVP summit I've also been putting the finishing touches on our Mastering Microsoft Surface™ Development with WPF class.  This class is going to be a lot of fun to teach.  First of all the class is chock full of WPF content.  WPF is a great platform for programming Surface applications (though I'd get some arguments about that from my XNA friends).  The course also has a ton of information about the Surface unit and the Surface SDK.   I don't know about you but I think the Surface is just plain fun to use.

image

The Surface was a huge hit during Ted Neward's  MVP summit party last week.

Creating apps for Surface

I have so many ideas for Surface applications.  The problem,of course, is that there is only so much time in a week to get everything done.  I want to have a new game ready by Devscovery NY.  We'll see if I can pull it off in time for the show.

 

image