Mat Velloso's Blog

Anything about Azure, Windows 8, XAML, Windows Phone, Visual Studio, .Net, Office 365, HTML5, SQL Server, well... you got the idea

April, 2012

  • Mat Velloso's Blog

    Running Windows 8 Consumer Preview on Samsung Slate 7? Check this


    Samsung has created a page to help you with drivers and everything. Enjoy :)




  • Mat Velloso's Blog

    Switzerland, Shape Conference, WinJS Windows 8, asynchronous and multi-threading


    Hello from Switzerland!


    What a beautiful, beautiful country this is. I’ve just arrived after a long flight from Seattle and on Monday I’ll be presenting at the Shape Conference, thanks to the invitation I’ve got from Ken Casada, a technology evangelist from the Swiss Microsoft team (or should I say “Schweiz”?). And Microsoft keeps allowing me to meet different places and people, how could I not love this job?

    Hey, here’s a view from my hotel room:


    I’m so inspired that I’ve decided to write about technology. (That’s how geeks function, we get inspired and then we do geeky things)

    So let’s talk about WinJS promises and a typical misconception: Asynchronous versus multi-threaded.

    Now if you have read about WinJS promises you have learned it provides a very nice way of running tasks asynchronously. Here’s an example:




    I really like the syntax there. It’s clearly saying: “Hey, download this feed first and then update the UI”.

    The UI won’t freeze because the download is happening, so the experience is not harmed because you’re waiting for the 3G network to do whatever it has to do.

    Now that’s asynchronous to me. But wait, let’s see what happens here:


    My “KillMyCPU” function does what it says: it keeps things busy. Now the thing is, just because promise provides asynchronous capabilities it doesn’t mean it is not single-threaded. When you run this code you’ll notice everything freezes. Because the KillMyCPU function asks for all the CPU’s availability, forcing that thread to not allow anything else to run.

    So what we do in such cases?

    First, you don’t take 100% CPU in a for..loop :) 


    But let’s assume you still want to create additional threads. There are ways around this. One that I particularly like is mixing JavaScript and C# together. Why I like it? Because mixing JavaScript and C# is what web developers have been doing for a long time. They are used to it so this is not so different: You take advantage of what is most powerful about each.


    So what you do:


    1-Create a Metro JS/HTML project

    2-Create a Metro C# class library

    3-Go to the properties of the Metro C# class library and change it to generate a WinMD file

    4-At the Metro JS/HTML project, add a reference to the class library

    5-Create a class like this:

    The class has to be sealed for the WinMD to compile and all it is doing is doing the same for..loop using the ThreadPool.

    6 – From the JavaScript code, call the class:

    Now if you run this you will notice the UI is not being frozen because of this code. Of course this increases the complexity since now you have to deal with multi-threading, multi-language, etc. Still it shows you can actually combine the power of these two languages together.


    Although, it still makes no sense to create a Metro application that consumes so much CPU. Think about tablets, low spec machines and think about the overall experience you want to provide to the user. For that kind of stuff, 100% CPU isn’t a good idea, is it?


    But the point here is to understand that asynchronous and multi-threaded are very different things.


    Now I’ll have to disconnect so I can enjoy Zurich while I’m still here.


    Auf Wiedersehen!


Page 1 of 1 (2 items)