Tuesday 2 November 2010

C# 5.0 and the sample that made me go “wow!”

In the Visual Studio Async CTP there is one sample project called 101 Asyncs. This is a browser displaying 101 code-snippets involving the new C# 5.0 async functionality. As I was flicking through I found the following sample, which I think speaks volumes for the elegance and simplicity of the feature:

public async void AsyncSwitchToCPU()
    Console.WriteLine("On the UI thread.");

    // Switch to a thread pool thread:
    await new SynchronizationContext().SwitchTo();  
    Console.WriteLine("Starting CPU-intensive work on background thread...");
    int result = DoCpuIntensiveWork();
    Console.WriteLine("Done with CPU-intensive work!");

    // Switch back to UI thread
    await Application.Current.Dispatcher.SwitchTo();                

    Console.WriteLine("Back on the UI thread.  Result is {0}.", result);

public int DoCpuIntensiveWork()
    // Simulate some CPU-bound work on the background thread:
    return 123;

Using Ready-made Virtual PCs to try out the Visual Studio Async CTP

The Visual Studio Async CTP containing the preview of C# 5.0 which was all the talk at Microsoft PDC 2010 installs on top of Visual Studio 2010. If, like me, you’re wary of trashing your main development machine, you might like to try out the ready made Visual Studio Virtual Machines that Microsoft provide. They are a pretty hefty download (over 9 GB), but I set my download manager to work on the task just after sitting down at my desk this morning, and it was done by mid-afternoon.

Having a Windows Server 2008 Virtual Machine ready built with Visual Studio 2010 (and 2008), Team Foundation Server 2010, SQL Server 2008 and Microsoft Office is a huge time-saver. Though they are all time-limited versions, you can apparently use your MSDN product keys to activate them into perpetual mode.

They come in three flavours, one for Hyper-V, one for Virtual PC 2007, and one for Windows 7 Virtual PC. If you hop over to Brian Keller’s blog he has a some lists of URLs that you can paste into your download manager to make the download job pretty painless, and then some instructions to get you started. One thing to note, once you’ve booted the machine, is that all the user accounts have password “P2ssw0rd”.

Having booted into the Virtual PC, you will need to install the Silverlight 4 SDK and then, of course, the Visual Studio Async CTP itself. Both are pretty small downloads and quite quick to install.

Have fun!

Monday 1 November 2010

WPF to support hosting of Silverlight controls in vNext

To the great consternation of the blogosphere, Joe Stegman’s planned session on Silverlight quietly disappeared from the Microsoft PDC schedule, and no new announcements were made on that front. Instead we got a pre-recorded session from Rob Relyea entitled WPF Today and Tomorrow.

Microsoft adoption

Early on in his talk Rob reminded us that Microsoft is making the ultimate commitment to WPF in that many of their latest products are built on it, including the Expression Studio suite, Visual Studio, and most recently Web Matrix [see the demo in Rob’s talk]. He also hinted that there are other products on the way which cannot be announced yet.

As part of these efforts across Microsoft a number of controls have been developed which Rob feels would be widely applicable(one of which is WPF equivalent of the Silverlight ChildWindow) and he intends to work with the respective teams to release these components to the web.

Silverlight Integration

One welcome announcement Rob made was that the next version of WPF will support hosting of Silverlight controls [watch his demo]. Today, of course, this is somewhat possible by hosting a Web Browser inside WPF, then navigating it to some Silverlight content. What Rob announced was something deeper than that, in two ways. The first is that they want to enable good communication between the WPF host and the Silverlight components, including sharing data.

The second improvement was part of a bigger announcement: they are planning to solve the airspace issues with hosting embedded Hwnd content [watch]. This was something I didn’t expect – in fact, I thought the airspace issue was a problem here to stay.

To briefly recap what the airspace issue is: in current versions of WPF, whenever you need to embed Win32-based content inside WPF, the embedded content always ends up sitting on top of the WPF stuff. This is because Win32 Hwnds don’t layer well with WPF elements – they can’t share airspace. You also can’t rotate or scale the Hwnds or adjust their opacity like you can with WPF elements.

Rob announced that Microsoft will be making a large investment, and will remove the airspace restrictions. And I can see why it will be a large investment. From what Rob said [watch] it seems that they way they plan to tackle this is by rendering Hwnds off-screen so that their visuals can be composited into the appropriate place in the visual tree, then any keyboard our mouse input intended for the control will have to be redirected from the WPF surface back to the Hwnd. A tough challenge indeed – but with great rewards for anybody looking to port legacy applications.

Amongst other things that Microsoft are considering for WPF vNext, Rob mentioned the following:

  • Proper support for Grouping in ItemsControls when doing UI Virtualization [watch]
  • Improved support for data-binding to collections and properties that get updated in a background thread [watch]. For collections, this will involve adding APIs to allow the data-binding engine to interact with the locking scheme used in the collection.

C# 5.0 – Other News

Compiler as a Service

In the last 10 minutes of his talk on the future of C# 5.0, Anders Hejlsberg gave an update on a project that he first mentioned back at PDC 2008 – Compiler as a Service [watch]. Anders confirmed that what was a prototype back in 2008 is now being worked on in earnest. It has a codename – “Roslyn” (he didn’t actually say this, but you’ll see it appear as a namespace in the code samples, and in one of the Visual Studio tabs), but no ship dates as yet.

As Anders put it, during the compilation process, the compiler builds up a huge amount of knowledge about what your code actually means – and then throws it all away. The idea of Compiler as a Service is give you a Language Object Model encapsulating exactly the same knowledge about your code as the compiler has - right down to the finest semantic details like what an identifier means in a particular context, or precisely how a method call will bind at run time. image

It seems that this Language Object Model will be available in a number of different ways, including to Visual Studio Extensions, which will be able to use it for things like performing Refactorings, or formatting code intelligently.

Anders showed one example of a Visual Studio Extension that could add outline regions to if/else blocks [watch]. Sadly, the extension failed to run in Anders demo, but it still conveyed the simplicity and power of the Language Object Model.imageIn another example he showed how the Language Object Models for VB.Net and C# could be used together to create a Visual Studio extension allowing you to copy C# code and paste it as VB.Net [watch]. “I’m not saying this is super-trivial”, remarked Anders, “I mean, there is a couple of thousand lines of code here. But it’s not a complete compiler at all – it’s relatively modest.”

There is more to come

In answer to a question at the end of the session [watch], Anders confirmed that there will be other features added to C# 5.0 that they’re not ready to talk about yet. He did hint that the kind of areas they are exploring were Immutability, Isolated State and Pure Functions – all features associated with ensuring that programs continue to work correctly when parallelism is introduced. Check out the Programming Languages Panel session if you want to hear Anders talk in a little more detail about this.