Thursday, January 24, 2013

Windows Store Apps with F# (part 2) : async/await

In my last post we created an windows apps and discussed the F# and C#/XAML interoperability. We discovered two issue:
  1. Long running calculations made the UI frees.
  2. The improper handling of the input made the app crash.
The way we deal with issues is an architectural decision. One can choose to have a slim F# library. The only responsibility is to handle the business logic, nothing else. In our case this is a math library. There is nothing wrong with decision, the other concerns are handled at the C#/XAML site by C# developers and XAML designers. We could resolve the first issue by wrapping the calculation in a task and await it. We could solve the input issues by writing a handler that will handle TextBox TextChanged  event.
If this is your decision the post ends here.
Or we could decide to minimize the C# code and solve all issues at the F# site. That is the way we will continue.
We start by where we left last time.
We first fix the second issue.
We delete in the MainPage XAML the last two TextBlocks and add a new one:
<TextBlock Grid.Row="3" Grid.Column="1" Grid.ColumnSpan="3"  x:Name="result" Style="{StaticResource FSharp}" />

and add a text wrapper function to a new F# file called Wrapper.
module MathLib.Wrapper
open System
let IsPrimeText text =
let success, number = Int64.TryParse(text)
if success then
Prime.IsPrime number then
number.ToString() + " is a prime number"
number.ToString() + " is not a prime number"
text + " is not a valid number"

Now we can change the OnClick event handler in
private void OnClick(object sender, RoutedEventArgs e)
result.Text =

All the code to handle the text input and displaying the result are now in the wrapper. Text input does not crash the app.


In a real project we could put more logic to del with input in the wrapper functions.

Next we have to deal with long running calculations. The new C# way is to use async/await. so we add async to the OnClick handler. We get a warning.

The warning tells us that we need to await something, and this should be a Task.

So we create a second F# wrapper:
open System.Threading.Tasks
let IsPrimeTask text =
fun _ -> IsPrimeText text)

And call this function with await:
private async void OnClick(object sender, RoutedEventArgs e)
result.Text =
result.Text =
await Wrapper.IsPrimeTask(inputValue.Text);

When we start the application and check 756771235126757131.


The task is started and we can still update the UI. So the issues are solved.

Remark: This works because it is .NET interoperability. One should use the WinRT the  interface  IAsyncOperation<string>.
private async void OnClick(object sender, RoutedEventArgs e)
result.Text =
IAsyncOperation<string> operation = Wrapper.IsPrimeTask(inputValue.Text).AsAsyncOperation<string>();

result.Text =
await operation;

This interface does not seem to be available in the F# library. Fortunately we can not reference a F# Portable Library in a JavaScript App:


So this is not an issue at the moment.

Next time we will look for ways to get ride op the event handling code by looking at data binding and MVVM.

No comments:

Total Pageviews