C# 5.0 Async/Await Demo Code

I’ve published the sample code I use to demonstrate the use of async/await in C# 5.0. You can find it here.

Projects

PauloMorgado.AyncDemo.WebServer

This project is a simple web server implemented as a console application using Microsoft ASP.NET Web API self hosting and serves an image (with a delay) that is accessed by the other projects.

This project has a dependency on Json.NET due to the fact the the Microsoft ASP.NET Web API hosting has a dependency on Json.NET.

The application must be run on a command prompt with administrative privileges or a urlacl must be added to allow the use of the following command:

netsh http add urlacl url=http://+:9090/ user=machine\username

To remove the urlacl, just use the following command:

netsh http delete urlacl url=http://+:9090/

PauloMorgado.AsyncDemo.WindowsForms

This Windows Forms project contains three regions that must be uncommented one at a time:

Sync with WebClient

This code retrieves the image through a synchronous call using the WebClient class.

Async with WebClient

This code retrieves the image through an asynchronous call using the WebClient class.

Async deadlock

This code how async operations can still deadlock.

Async with HttpClient with cancelation

This code retrieves the image through an asynchronous call with cancelation using the HttpClient class.

PauloMorgado.AsyncDemo.Wpf

This WPF project contains three regions that must be uncommented one at a time:

Sync with WebClient

This code retrieves the image through a synchronous call using the WebClient class.

Async with WebClient

This code retrieves the image through an asynchronous call using the WebClient class.

Async deadlock

This code how async operations can still deadlock.

Async with HttpClient with cancelation

This code retrieves the image through an asynchronous call with cancelation using the HttpClient class.

Feedback Requested: Usability Of The Remarks Section In Reference Topics

Microsoft is planning to expand the Remarks section of selected types in the .NET Framework Class Library to provide detailed usage information and code examples. (For an example, see the Remarks section for the System.String class.) In the current design, the Remarks section isn’t easily discoverable, because member tables take up a lot of screen real estate.

Some alternate page designs are proposed to address this problem.

Click here to provide your feedback.

Windows 8 SDK Customer Satisfaction Survey

Windows 8 introduces a number of innovations in the way information is delivered to developers. Microsoft would like to know how well these are working for you, and where they can make further changes to improve your experience.

To review the site before you complete the survey, visit the Windows 8 Dev Center. In particular, have a look at the section called Learn to build Metro style apps.

A few questions in the survey are about about how the Windows 8 site experience compares to the iOS and Android sites. If you aren’t an experienced iOS or Android developer, feel free to skip these parts. But, if you’ve made apps for those platforms, or if you’d like to compare site features based on just a browse through those sites, Microsoft would like to hear your opinion.

The survey will be available here until July 27, 2012.

Breaking Changes In Argument List Evaluation In C# 5.0

The C# Language Specification states on §7.5.1.2 that “(…) the expressions or variable references of an argument list are evaluated in order, from left to right (…)”.

So, when this code is compiled with the C# 4.0 compiler:

static void M(
    int x = 10,
    int y = 20,
    int z = 30)
{
    Console.WriteLine(
        "x={0}, y={1}, z={2}", x, y, z);
}

static void Main(string[] args)
{
    int a = 0;

    M(++a, z: ++a);
}

and run, this unexpected output is obtained:

x=2, y=20, z=1

In fact, fixing this compiler flaw was the cause of one of the few breaking changes introduced in C# 5.0.

Using the 5.0 compiler, the expected result is obtained:

x=1, y=20, z=2

To avoid this type of surprises, expression evaluation should be avoided in argument lists.

With this code:

int a = 0;

int i = ++a;
int j = ++a;

M(i, z: j);

the same result is obtained for both C# 4.0 and C# 5.0:

x=1, y=20, z=2

How To: Make XAML Content Globalizable When Using Value Converters

(This content was written based on Silverlight for Windows Phone, but might be valid for generic Silverlight.)

There are a many articles on MSDN (and all over the Internet) about globalization and localization of Silverlight applications in general and specifically Windows Phone 7 applications but I haven’t found any that uses a value converter.

Continue reading How To: Make XAML Content Globalizable When Using Value Converters

People+ For Windows Phone

Recently, I’ve released a Windows Phone application to explore and use contact information.

In this application I used components and/or guidance from:

In future posts, I’ll be showing some tips, tricks and components I’ve used in this application.


People+ for Windows Phone
People+ for Windows Phone

Explore and use your contacts’ information.

Search contacts by:

  • name
  • phone number
  • e-mail address
  • physical address
  • websites
  • company
  • job title
  • significant others
  • children
  • notes

* Trial limitations The trial version is fully functional but will show a purchase reminder and might show ads.

* Application limitations

The Windows Phone system imposes some limitations on application developers.

It is not possible to:

  • access twitter information
  • access linkedin information
  • uniquely identify the contact
  • change or provide access to change the contact information

* Privacy statement This application does not make any use of the contacts information other than displaying and searching it. When ads are displayed, however, device information might be sent to the ad provider (smaato Privacy Policy).

Hash Function Added To The PredicateEqualityComparer

Sometime ago I wrote a predicate equality comparer to be used with LINQ’s Distinct operator.

The Distinct operator uses an instance of an internal Set class to maintain the collection of distinct elements in the source collection which in turn checks the hash code of each element (by calling the GetHashCode method of the equality comparer) and only if there’s already an element with the same hash code in the collection calls the Equals method of the comparer to disambiguate.

At the time I provided only the possibility to specify the comparison predicate, but, in some cases, comparing a hash code instead of calling the provided comparer predicate can be a significant performance improvement, I’ve added the possibility to had a hash function to the predicate equality comparer.

You can get the updated code from the PauloMorgado.Linq project on CodePlex,

Adventures in C#, .NET and Software Development in general