« October 2010 | Main | December 2010 »

November 2010

WPF = Silverlight+

Should I use WPF or Silverlight? What’s the future of WPF? So many news on Silverlight, but none on WPF? I’m hearing a lot of questions related to the choice of these technologies. If WPF is renamed to Silverlight+, I think the questions would end Smile

Before its release Silverlight was known by the codename WPF/E. WPF/E = WPF Everywhere. This codename gives the relation between WPF and Silverlight. Of course this name didn’t sound flashy so a different name was found: Silverlight. Good for Silverlight, not so good for WPF.

WPF Everywhere - recent news on Silverlight have been on a strategy shift: Our Strategy with Silverlight has shifted. It’s not exactly “everywhere” anymore. To target every platform, HTML is best. Silverlight has big advantages in media and LOB applications; it has a bright future, a big development team and great features to come. Silverlight is much more feature-rich than HTML. Of course with Silverlight it is necessary to think about the client where the application should run. This is not a big difference to HTML. With HTML it is also important to have different screen sizes in mind to give a good user experience.

What platforms can be used with   Silverlight? Silverlight is available on Windows, on the Mac, on the Windows Phone, Nokia Symbian, with Windows Embedded… A Silverlight technology – named Moonlight – is also available on Linux.

There’s something common to these Silverlight technologies. XAML, some controls, writing code with C#, sometimes Visual Basic… [ Developing Silverlight for Windows Phone is soon possible with Visual Basic. ]

Not all the Silverlight controls are available on all platforms. Some are just not there, yet; others don’t make sense on different platforms. With all these Silverlight offers I still want to make use of the platform. So different platform versions of Silverlight support different features. For each of these devices different toolkit exists. From Silverlight with Windows Phone 7 I like to make phone calls, send SMS messages, use the accelerometer for motion sensing, use push notifications… Running Silverlight on the desktop I would like to access the desktop, run elevated to do tasks where more privileges are required, call into Microsoft Office… And I have a bigger screen than on the phone. Did you already use a Windows Phone 7? Panorama controls, the application bar… The user expects specific application behavior on a Windows Phone 7, behavior that is so different to the desktop.

WPF has even more capabilities than Silverlight. Some Silverlight controls are not yet available with WPF, but there are several features that are currently just not possible with Silverlight. Real 3-D, flow- and fixed documents are just possible with WPF…, and the complete .NET Framework can be used. Developing with Silverlight sometimes I’m really missing some core .NET features. Of course I find my way around it, but it would be easier with the full framework. Using the complete .NET Framework, WPF has a big advantage to Silverlight.

So what technology should be used depends on what features are required, what platforms must be supported. Every device has different characteristics and also different usage scenarios. It’s always necessary to build the UI for the client in mind. WPF is the big brother of Silverlight. If the features just available with WPF should be used, WPF is the best option. Expression Blend, Visual Studio, Web Matrix are just some of the products done by Microsoft with WPF. If it’s not clear if the WPF features are required from the start, it is easier to begin with Silverlight and change to WPF instead of the other way around.

Service-functionality can be shared between all these application types. It’s easy to create a service that is used both from WPF and Silverlight clients. Different bindings to these clients can be a good option. And it’s also possible to share assemblies between WPF, Silverlight for the desktop, and Silverlight for the phone. Today that’s possible with a Silverlight 3 class library. Nowadays it’s necessary to be careful not using the APIs that are not available on the targeted platforms. A future version of Visual Studio makes the assembly sharing easier as shown from Shawn Burke in the PDC-presentation 3-Screen Coding: Sharing Code between Windows Phone, Silverlight, and .NET.

I’m seeing WPF just as a big brother of Silverlight. And WPF would have an easier life it it’s renamed to Silverlight+.


CN innovation

Follow me on twitter

Async Programming with .NET and C# v.next

As more and more cores are the norm with PC’s (I also expect to have smartphones with multi-core CPUs soon), programming with multiple threads gets more and more important.

This blog post gives an introduction to a new C# feature for working asynchronously by comparing it to older technologies.

.NET has some history on async programming:

  • With .NET 1.0 the async pattern was available to have a standard way to deal with long running methods.
  • .NET 2.0 added the async component pattern or async event pattern that made it easier with Windows-based programs. The async pattern from .NET 1.0 had the disadvantage that a callback method that is invoked when the long running operation completes makes use of a background thread and thus it is necessary to switch to the UI thread that owns the synchronization context.
  • With .NET 4 several enhancements have been done to write asynchronous methods. .NET 4 added a standard cancellation mechanism, the Task library and Parallel Lf deINQ.

C# so far only had the lock keyword to synchronize multiple threads. This changes with the next version of C# that offers async and await keywords.

Before showing the upcoming C# feature let’s compare this with older ways of programming asynchronously. The first sample shows downloading some data in a synchronous manner with the WebClient class. DownloadData can be used to get data from a server into an byte array.

private void OnSync(object sender, RoutedEventArgs e) { byte[] data = null; textStatus.Text = string.Empty; using (WebClient client = new WebClient()) { data = client.DownloadData(address); textStatus.Text = "Completed"; } }

Doing this from within a Windows application, the user interface is stalled while the synchronous method DownloadData waits until all the data is retrieved from the server.

To not block the UI, the data can be retrieved in an async manner. The second sample shows the use of the async pattern. This pattern defines a Begin and End prefix to the methods used for async calls and makes use of the IAsyncResult interface.

Contrary to the HttpWebRequest class, the WebClient class does not offer this pattern by itself. The HttpWebRequest class could be used to invoke the methods BeginGetResponse and EndGetResponse that follow the async pattern. To invoke any synchronous method in an async manner also a delegate can be used. This is shown in this sample.

For invoking the method WebClient.DownloadData a delegate that requires a string parameter and returns a byte array is defined with the Func<string, byte[]> delegtate. The delegate instance named download references a lambda expression that gets a string parameter and returns the result from the WebClient.DownloadData method. The implementation of the lambda expression is invoked asynchronously by calling the BeginInvoke method from the delegate instance. The first argument of this method defines the input data for the method that is invoked by the delegate. With this argument value the method that is referenced by the delegate is invoked from  thread pool thread.

The second parameter is of type AsyncCallback that is void and requires an IAsyncResult parameter. This parameter defines the method that is invoked as soon as the asynchronous method completes. Invoking the method EndInvoke from the delegate instance, the result (in this case a byte[]) is returned.

Invoking methods and properties from UI elements cannot be done directly, as the method that is assigned to the AsyncCallback is running within a background thread. To switch back to the UI thread, with WPF the Dispatcher property that returns a DispatcherObject can be used. The Invoke method of the DispatcherObject requires a delegate with the first parameter. Here, the Action<string> delegate is used to pass a string to the method.

private void OnAsync1(object sender, RoutedEventArgs e) { textStatus.Text = string.Empty; byte[] data = null; using (WebClient client = new WebClient()) { Func<string, byte[]> download = addr => { return client.DownloadData(addr); }; download.BeginInvoke(address, new AsyncCallback(ar => { data = download.EndInvoke(ar); Dispatcher.Invoke( new Action<string>(s => textStatus.Text = s), "Completed"); }), null); } }

Because of this complexity in special with Windows applications where it is necessary to get back to the UI thread the async event pattern was introduced with .NET 2.0. This pattern defines that a async method is postfixed with the name Async and defines an event with the postfix Completed that is invoked as soon as the async mehtod finishes. The big advantage in contrast to the callback method with the async pattern is that the event handler that is assigned to the Completed event is invoked from a thread that has the synchronization context. With WPF or Windows Forms applications this is the UI thread.

Doing the same sample as before with the async event pattern methods from the WebClient class can be used directly as this class offers an implementation of this pattern. The DownloadDataCompleted event a lambda expression is assigned to get the result from the async call, and change that status information in a TextBlock directly.

private void OnAsync2(object sender, RoutedEventArgs e) { textStatus.Text = string.Empty; using (WebClient client = new WebClient()) { client.DownloadDataCompleted += (sender1, e1) => { byte[] data = e1.Result; textStatus.Text = "completed"; }; client.DownloadDataAsync(new Uri(address)); } }

With WPF and Windows Forms the async event pattern is a lot easier to deal with contrary to the async pattern. However, it is still very different to programming synchronously. Before the async method is invoked it must be defined what happens when the method is finished which can make it quiet complex reading such code.

A goal of the new C# keywords is to write asynchronous code in a way that is similar to programming synchronous code. This sample requires the use of the Visual Studio Async CTP and a reference to the AsyncCTPLibrary assembly. This assembly contains an extension method to WebClient named DownloadDataTaskAsync that returns Task<byte[]>. Instead of using the task that is returned from this method the await keyword is used that means we’re waiting on the result of the task that is of type byte[]. Waiting here with the await keyword does not block the thread. Instead, the UI thread can do some other actions and gets informed by a message when the task is completed, so it can continue with the result that is returned from DownloadTaskAsync.

private async void OnAsync3(object sender, RoutedEventArgs e) { textStatus.Text = string.Empty; using (WebClient client = new WebClient()) { byte[] data = await client.DownloadDataTaskAsync(address); textStatus.Text = "Completed"; } }

Programming asynchronously this way is very similar to programming synchronously. The order of the code is similar to synchronous programming and it still behaves asynchronously.

async and await are just language features. It was possible to do the same with the current version of the compiler, the code just reads more complex.

This is really a great feature of the next version of the C# compiler. Expect more information about this in my blog.


CN innovation

Follow me on twitter

PDC Highlights bei der User Group am 18. November

Am 18. November gibt es Highlights von der PDC 2010 in Redmond bei der .NET User Group Austria. Würde mich freuen viele dort zu sehen!

Donnerstag, 18. November 2010
.NET User Group Austria

Global Knowledge
Gutheil Schoder Gasse 7a, 1101 Wien
Bitte um Anmeldung: ug@christiannagel.com
16:30 Zusammenkunft und Diskussionen, Video von Steve Ballmer
18:00 Präsentation von Christian Nagel, Highlights von der PDC 2010

Diesmal gibts den Start schon etwas früher mit der PDC Keynote von Steve Ballmer. Die Präsentation mit PDC Highlights startet dann um 18 Uhr. Diejenigen die die Keynote schon gesehen haben (z.B. in der PDC Kino-Veranstaltung) können auch erst um 17:30 kommen Winking smile


Highlights von der PDC:

  • Windows Azure
  • IE9
  • .NET Framework Erweiterungen
  • C# v.next
  • und vieles mehr!



CN innovation

My take from PDC 2010

My previous blog posts were summaries about PDC day 1 and PDC day 2. This is now a conclusion what I take with me from PDC 2010 into the next months.


Currently I’m working from the UI side mostly with WPF and Silverlight on the desktop, and Silverlight for Windows Phone 7. I will continue to do so.With my new LG WP7 it gets a lot easier to write phone applications. What I’ve seen is most important to writing WP7 applications is using a WP7.

Bob Muglia just clarified the strategy on Silverlight in this blog post with this emphasize:

  1. Silverlight is very important and strategic to Microsoft.
  2. We’re working hard on the next release of Silverlight, and it will continue to be cross-browser and cross-platform, and run on Windows and Mac.
  3. Silverlight is a core application development platform for Windows, and it’s the development platform for Windows Phone.

Probably MIX 2011 April 12-14, 2011 is a good date for an upcoming Silverlight release.

Windows Azure

Some of my Silverlight projects are hosted in Windows Azure. I will continue to do so. I will also dive into the new features of Windows Azure as listed on the Windows Azure Team blog, such as Windows Azure Connect (CTP available later this year), ‘Extra Small’ Windows Azure instances, the new Windows Azure portal and diagnostic information.

HTML 5 and IE 9

I’m already using IE 9 for some time. Installed the IE9 Preview 6 as well. So far I didn’t do a lot with HTML 5 other than trying the samples. I will dive deep into this technology, together with technologies that I’m already using such as ASP.NET MVC – now version 3 with the Razor view engine.

For sure MIX 2011 April 12-14, 2011 is also a good date on more news on HTML 5 and IE9.

Languages and Frameworks

And of course I’m already testing the next version of C# with async and await keywords. Also diving into the WCF and WF enhancements. Already thinking about the next edition of Professional C# 4 with .NET 4.


CN innovation

Follow me on twitter