Blog Archives

Debugging Windows Phone 8 apps with the emulator using Hotel Wi-Fi

Have you ever tried to run/debug your Windows Phone 8 apps when using Hotel mode Wi-Fi?

Hotel-mode Wi-Fi are guest access Wi-Fi routers that do not require a password to connect, but rather the first time you attempt to open a web-site you are routed to a login form, where you’re prompted with either a confirmation page or a user-id/password page.

So, where’s the problem?

You have your Visual Studio 2012 open, you already connected to the Hotel-mode router and made sure to open a page (google.com/microsoft.com/whatever) in order to go through the intermediate page and get internet connected.

Now, you launch your app, causing the emulator to load (takes about forever…) and the app to run just to find out that your app cannot connect to the internet!…

Checking again, you discover that your computer somehow reset the connection with the router and you have to go through that login page again. But, that’s not enough, as the connection will keep resetting every few seconds, making it almost impossible for you to run and debug your app, assuming it requires internet connection.

So what can you do if you just have to debug or demo your app from the emulator on such a network, assuming you do not have a Windows Phone 8 device around?

After many tries (and failures), I found only two possible solutions:

The first is to use your phone’s mobile data and tether the internet connection through Wi-Fi to your computer. But, mobile data doesn’t come cheap and you would rather use that complimentary Wi-Fi for your tests.

The solution I found was to run a USB tethering app on my Android phone, have the phone connect to the Hotel-mode Wi-Fi and tether the connection to the computer using cable.

That way, the connection is not being reset when using the emulator and you can run/debug your Windows Phone 8 app.

Advertisements

Demo code from my “Azure as the backbone” talk @DevTeachConfere

Thanks to everyone who attended my “Azure as the backbone” web-api based talk today @DevTeachConfere in Montreal.

Demo code can be found on my sky-drive here.

See how easy it is to create a web-api based app using the Visual Studio 2012 wizard, deploy it to Azure and then create Windows 8 and Windows Phone 8 apps that uses the service from within Azure.

Going through these examples you’ll see that there was very little code required in order to get these Windows 8/Windows Phone 8 apps connected to the web-api based Azure app.

Please note that the web-api MVC 4 project uses a bunch of NuGet packages, automatically installed by the wizard within my project.

Write me or comment for questions!

Demo code from my Practical Kinect session @DevTeachConfere

Thanks to everybody who attended my session about practical kinect at the Dev-Teach conference (@devteachconfere) today, to hear about the Kinect SDK, the different types of interactions and the way to implement them in Windows.

The demo code can be downloaded from my SkyDrive: here

The demo code includes two solutions:

KinectPointers: demonstrating how to react to hand movement by moving the mouse and generating a mouse click upon  a 1 second hover at a certain position

KinectWPFDemo: demonstrating how to create a media-player that uses postures to play, pause, fast-forward, rewind and alter the playback volume.

I’d love to hear your suggestions for practicle apps using the Kinect SDK!

Kinect SDK and Windows 8

Microsoft Kinect SDK is claimed to work under Windows 8.

Well, before you run-off starting to develop your next Kinect multi-touch experience for the Windows 8 UI, it is important that you know that there are limitations to that.

Although using the latest Kinect SDK driver, released October 2012, you can develop and run apps under the Windows 8 operating system, it only works for developing traditional desktop apps, meaning what you would call “Windows 7 apps” and not what is called: “Windows Store apps”, also formerly known as “Metro-style apps”.

The Kinect libraries are not compatible with Windows Store apps and therefore you cannot add a reference to a legacy library that works against the Kinect SDK. Visual Studio 2012 prompts you with an error message saying: “Unable to add reference”.

I’m currently working on a work-around that may enable a small portion of the Kinect features in Windows Store apps and will surely update when I come-up with something.

Latest Kinect SDK information and download to be found here: http://www.microsoft.com/en-us/kinectforwindows/develop/new.aspx

What’s new in Windows Azure?

In June 2012 Microsoft released a major update for Windows Azure, including IaaS (mounting virtual machines), websites on a shared infrastructure and a variety of frameworks, virtual networks, a new SDK, and support for many new languages including Java, Python, PHP and JavaScript.

This release puts Microsoft’s flagship cloud offering in great competition with Amazon in the IaaS market and with Google App Engine in the website market.

On the coming Sela Dev Academy I will be training a whole day tutorial in which I will present the new Azure portal and demonstrate the new features in the SDK.

Are you already using Azure? Are you developing against the SDK?

If you’re using Amazon or Google’s platforms you might want to hear this one out.

From Windows 8 to the Store

So, you’ve created an amazing new app for the Windows 8.

Everybody loves it and tell you that you should make it public and that people should pay you to use it.

But, how do you do that? It sounds complicated, isn’t it so?

It sounds like something companies does, not private people or small groups!

Well, it’s not that complicated and surely everybody can do that. It just takes some learning and a few correct steps and you’re there, at the app store, with your brand new app waiting to be downloaded and purchased!

So, to help you jump the hedge, I’ll be giving a lecture @devacademytoronto titled: “From a New Windows 8 Project to the Store“, in which I’ll show the list of actions to be taken in order to have an app in the store, explain the  methods and show the pitfalls to avoid along the way.

See you there!

Lecturing “Creating Powerful Windows 8 Applications with C++/CX for WinRT” at the North Toronto User Group, June 4th, 2012

In addition to the lecture I’m giving at the “East of Toronto .Net User-Group”, of-which I wrote here a few days ago, I’ll be giving another lecture on June 4th, 6:30pm, at the “North Toronto .NET User Group” in the North York Central Library, Ontario @NorthTorontoUG, labeled: “Creating Powerful Windows 8 Applications with C++/CX for WinRT”.

C++ has been receiving new attention from Microsoft during the last few years.

Based on the newer C++ 11, the non-standard extension: C++/CX, is the language extensions used for WinRT development on the Windows 8.

C++/CX and C++ 11 new language features adopt many of the features we learned to appreciate on the C# language over the years, but has the full power of the C++ language.

In this session we will get to know the newer Visual Studio 2011 (Beta), go over C++ 11 language features and see how to build WinRT applications with C++/CX.

If programming in C++/CX for Windows 8 interests you, I’ll be glad to see you in the session.

For more details, please see the event information here.

See you there!

Lecturing “Querying and Manipulating EDMs” at the East of Toronto .NET User Group, June 8th, 2012

On June 8th, 6:30pm, I will be giving a lecture at the “East of Toronto .NET User Group” in  the Pickering Central Library, Ontario @easttorontoug, labeled: “Querying and Manipulating EDMs”.

The Entity Data Model, being the backbone of Entity Framework, hides the skeleton of our conceptual entities, as well as the actual data-source structure and the mapping between them, all wrapped in a three layer model.

It is essential to understand how these layers interact with each other, how to access the data from, and what is being performed under-the-hood. In this session we will go through and understand the various layers and their roles, learn how to code against the data model from within LINQ & Entity-SQL (ESQL), and understand the process that is performed while our queries make their way to the DB.

We will also see advanced querying methods, such as: Joins, Nested queries, Grouping, and more, and understand the implications and implementations of transactions and concurrency issues.

If you’re interested in understanding how the Entity Framework plumbing works, I’ll be glad to see you in the session.

For more details, please see the event information here.

See you there!

Demo code from my session about Live Tiles & Push Notification for Windows Phone on Microsoft TechDays Canada TV

I’d like to thank all of you who attended my session about Live Tiles & Push Notifications on Windows Phone on the Microsoft TechDays Canada TV!

Live tiles, channeled with Push Notifications, are important for your application’s user experience, and are very easily created in either Silverlight or XNA, and with a notification service residing anywhere on the cloud, with Azure being the better solution.

To make it even easier- you can find and download all of the code samples and demos from the session directly from my SkyDrive here.

I’ll gladly answer any of your questions posted here or at the Canadian Developer Connection LinkedIn group.

If you missed the session, you may find it on the TechDays TV site or directly on YouTube for your convinience.

You may also follow me twitter for more @HarariErez.

Captured variables, or- What did I just code?

Microsoft .Net and Visual Studio (especially the later versions) offer many automatic tools and code sugar that allows us to write code faster and safer.

But, every once in a while it is recommended to take a peek under the hood and see what the compiler had in mind, and how far it went from what we originally meant in our source code.

I do that using tools like the .Net Reflector, now a paid product and yet worth its price, at least for me, though there are other alternatives like the ILSpy.

Here’s an example-

I guess you’re already familiar with Lambda expressions. There are situations, though, where using Lambda expressions without caution might create severe bugs and/or performance hits.

Please see the following code, which you can copy directly to your Visual Studio to a fresh C# Console Application and test:

public static int CalculateFactorial(int number)
{
  int result = 1;
  for (int i = 2; i <= number; i++)
  {
    result *= i;
  }
  return result;
}

public static void Test()
{
  for (int counter = 1; counter <= 10; counter++)
  {
    ThreadPool.QueueUserWorkItem(state => 
      Console.WriteLine("Factorial of {0} is {1}", 
      counter, CalculateFactorial(counter)));
  }
}

It looks like we’ve created an efficient and somewhat elegant piece of code, aimed to calculate factorials of numbers between 1 and 10 while making the most of our computer processors, as we’re breaking these calculations to different threads.

So, where’s the catch?

Let’s open the .Net reflector and see the compiled code for the “Test” method:

public static void Test()
{
  for (int counter = 2; counter <= 10; counter++)
  {
    ThreadPool.QueueUserWorkItem(delegate(object state)
    {
      Console.WriteLine("Factorial of {0} is {1}", 
	counter, CalculateFactorial(counter));
    });
  }
}

OK, so we just found out that Lambda expressions are actually compiled into .Net
anonymous delegates. Lambda expressions are only code sugar and do not exist in the compiled CIL…

Big deal. Still, where’s the catch??

The first catch is that .Net reflector is smart enough not to display the actual code, but more of a sweetened version of the code.

In order to see the actual code we either need to view it in raw CIL, or change the reflector optimization mode to .Net 1.0.

So, here’s the same compiled CIL code once again, in decompiled to C# using in .Net 1.0 mode:

public static void Test()
{  
  WaitCallback CS$<>9__CachedAnonymousMethodDelegate1=null;  
  <>c__DisplayClass2 CS$<>8__locals3 = new <>c__DisplayClass2();  
  CS$<>8__locals3.counter = 2;  
  while (CS$<>8__locals3.counter <= 10)  
  {    
    if (CS$<>9__CachedAnonymousMethodDelegate1 == null)    
    {        
      CS$<>9__CachedAnonymousMethodDelegate1 =           
        new WaitCallback(CS$<>8__locals3.<Test>b__0);    
    }        
    ThreadPool.QueueUserWorkItem(
      CS$<>9__CachedAnonymousMethodDelegate1);        
    CS$<>8__locals3.counter++;  
  }
}

[CompilerGenerated]
private sealed class <>c__DisplayClass2
{  
  // Fields  
  public int counter;   
  // Methods  
  public <>c__DisplayClass2() 
  {
  }  
  public void <Test>b__0(object state)  
  {    
    Console.WriteLine("Factorial of {0} is {1}", 
      this.counter,       
      CalculateFactorial(this.counter));  
  }
}

Browsing through the above generated code, we can now learn that anonymous delegates
are also somewhat of “virtual” code elements that compile into something else, or- some more syntactic sugar!
Well, what the compiler actually does is to create a nested class with some kind of a garbled name that we cannot compile for our own classes
(this is in order to avoid code collisions with our own code).

This generated class holds a method that has the implementation of our original “anonymous
delegate“, to which the compiler directs the ThreadPool.QueueUserWorkItem delegate
parameter to run within an available thread.

We were writing .Net v1.0/2.0 code all along and we didn’t even know that!…

Now that we understand that, let’s see what happened to our “counter” variable from the original Lambda version:

In order to support it, the compiler added a public field to the nested class holding the implementation
method, by the name of “counter”. This counter field is being incremented once for
each loop cycle, just after the internal <Test>b__0 method is assigned for another
ThreadPool thread!

This means that the counter, being a shared resource, will probably make it all the
way up to 10 before the first thread even starts to run, thus causing all of these
threads to calculate the factorial of a 10, instead what we originally intended them to do:
calculate the factorials of every number between 1 and 10…

Such a problem is called: “Captured variables” as we captured the local variable within other threads, thus casuing it become a shared resource.

It and can be avoided by passing the counter as “state” parameter into the delegate, this way every thread owns its own private copy of the counter for each thread:

public static void Test()
{
  for (int counter = 1; counter <= 10; counter++)
  {
    ThreadPool.QueueUserWorkItem(state =>
      Console.WriteLine("Factorial of {0} is {1}",
      (int)state,
      CalculateFactorial((int)state)), counter);
  }
}

You can copy the above code to your Visual Studio and see that it now prints the correct results.

You can also go back to .Net Reflector to see what was built now by the compiler. It might just surprise you…