Month: January 2012

EvenTiles Until Now (A Windows Phone Development Series)

Looking at all the different topics that the EvenTiles series covers, I think it makes sense to at least have a table of contents for the series so far. For instance, if you are interested in reading something about tombstoning, it is not necessary to read through the entire series (although of course I invite you to do so anyway). At this moment, EvenTiles explains how to use Windows Phone Tiles, including Secondary Tiles. It also show how you can update content on your Tiles locally by using a Background Agent. Also don’t forget that all source code is available and each individual episode of EvenTiles contains a demo on video as well. The latest sample source code can be downloaded.

Here is the table of contents for the first 14 parts of EvenTiles:

Introducing the EvenTiles application

  • Creating a new project in Visual Studio 2010 Express for Windows Phone
  • Defining an Application Tile from within the WMAppManifest.xml file
  • Using the emulator to run the application

PageNavigation and ApplicationBar

  • Using Expression Blend to create an Application Bar
  • Navigating to different pages by using the NavigationService

Using the Silverlight for Windows Phone Toolkit in your application

  • Defining page transitions in XAML and using them in your application
  • Using styles in XAML
  • Declaring and using a ToggleSwitch control

Creating the Settings Page

  • Designing a Windows Phone page using Expression Blend
  • Executing code as a result of page navigation (using OnNavigatingTo and OnNavigatingFrom)
  • Using event handlers to execute code on user interaction with UI elements

Using IsolatedStorage

  • Storing data in between different times an application executes
  • Introducing the application life cycle
  • Using IsolatedStorageSettings to persist information

Using the Isolated Storage Explorer Tool

  • Examining the contents of our application’s IsolatedStorage

Fast Application Switching and Tombstoning

  • A Windows Phone Application’s life cycle
  • Saving application state when moved to the background
  • Restoring application state when returning to the foreground

More on Tombstoning

  • Maintaining Page State
  • Restoring a page to exactly the same situation it was when the application moved to the background
  • Restoring focus of input UI elements

Creating a Secondary Tile

  • Creating a Secondary Tile inside your application
  • Navigating from the Secondary Tile to the application
  • Determine if a Secondary Tile is currently visible on the Start screen of a Windows Phone through a lambda expression
  • Setting the front and back contents of a Secondary Tile

Background Agents

  • Different types of Background Agents
  • Properly creating a PeriodicTask
  • Starting and stopping a PeriodicTask
  • Relation between an Application and a Background Agent

Debugging Background Agents

  • Using conditional compilation to test Background Agents
  • Properly using the LaunchForTest method
  • Using the debugger to debug a PeriodicTask

The lifetime of PeriodicTask

  • Rescheduling a PeriodicTask
  • Exceptions that might be thrown when scheduling a PeriodicTask

Communication between an Application and its PeriodicTask

  • Passing data between an application and a PeriodicTask
  • Protecting variables against simultaneous access by different threads
  • Using a Mutex to synchronize threads

Exchanging data between an Application and its PeriodicTask

  • Using a file in IsolatedStorage to exchange data between an application and a PeriodicTask

Even though there are many more episodes of EvenTiles planned, attention will now move beyond tiles as we focus on using built-in applications and accessing device hardware.

EvenTiles from Start to Finish–Part 15

In the previous episode of this series about how to develop a Windows Phone application from scratch you learned how you can transfer data between an application and a PeriodicTask by using a file in IsolatedStorage. With that, the most important functionality of EvenTiles is in place. Today we will add the User Interface for an About Page to the application. Since we are not worrying about functionality yet, we will use Expression Blend exclusively in today’s part of the EvenTiles series.

An empty About Page is already part of our EvenTiles solution, and it can already be reached by clicking the corresponding icon on the ApplicationBar. The final About Page should look like this (with the upper 4 fields ready to fill in some data programmatically), some explaining text and a few buttons.

imageThe UI of the About Page is not that complex as you can see. It consists of a Grid Control containing a Border Control, a TextBlock and a number of Buttons, each on separate rows. All rows containing UI Elements have their height defined as auto. To complete the layout, two empty rows are also defined that fill up unused space on the page.

The Border Control contains another Grid Control which in turn contains 4 rows and 2 columns, all containing TextBlocks. The TextBlock that holds the version number of the application will be filled programmatically with the assembly version number that is defined for the application. The color of the Border Control and of the TextBlocks inside it is defined to be the accent color, so it will automatically change when the user changes the theme colors on the phone.

To keep the margins for text in each TextBlock consistent, we make sure to use one of the predefined styles on them from inside Expression Blend (all are using a PhoneTextNormalStyle). If you want to learn more about creating consistent user interfaces for Windows Phone pages, you should take a look at this excellent blog article from Jeff Wilcox with great tips on Metro Design.

image

In order to nicely divide a number of UI elements over the About Page, we are making use of the ContentPanel Grid. Inside that Grid, we define a few more Rows than we actually need, just to make sure that our layout looks good.

dump5

You can see that in the this screen shot, where all rows and columns are visible. The dotted lines show all rows and columns available, both for the Grid inside the Border and for the entire ContentPanel. In the ContentPanel you see two blank rows, they are used to fill up unused space on the page, by specifying their height to be of size “1*”. All the other rows are using the height they need by specifying their height to be auto.

 

 

 

 

 

 

The other thing that is noticeable, is the image to the left of the text inside the upper button. This button is created with a StackPanel defining its Content area. The StackPanel holds both an Image and a TextBlock and is defined as follows in XAML:

XAML to define ’email’ button
  1. <Button x:Name="btnEmail"
  2.         Grid.Row="4">
  3.     <StackPanel Orientation="Horizontal">
  4.         <Image Source="appbar.feature.email.rest.png" />
  5.         <TextBlock TextWrapping="Wrap"
  6.                     Text="Email feedback / suggestions"
  7.                     VerticalAlignment="Center"
  8.                     Style="{StaticResource PhoneTextNormalStyle}" />
  9.     </StackPanel>
  10. </Button>

You can also see that we simply re-use the email icon that ships with the tools for use in an ApplicationBar. Of course it is rather hard to show how to create a user interface inside a blog article. Most likely it makes more sense for this episode of EvenTiles to watch the accompanying video, in which you can see how the entire user interface for this page was created using Expression Blend.

Creating an About Page for a Windows Phone Application with Expression Blend

In the next episode of EvenTiles we return to actual programming, when we add functionality to the AboutPage to access MarketPlace, to submit application reviews and to use a Launcher to create / send an email message. You will also learn how to retrieve the application’s version number programmatically. Just make sure to return to this blog regularly or to subscribe to the RSS feed.

EvenTilesIf you want to see EvenTiles already in action on your Windows Phone, you can also install the latest version from Marketplace. Remember that this application is not meant to be extremely useful, although it contains similar functionality that “serious” applications have. Just go ahead and get your free copy of EvenTiles from Marketplace at this location: http://www.windowsphone.com/en-US/search?q=EvenTiles (or search on your phone for EvenTiles in the Marketplace application).

MessageBox, Navigation and Application Life Cycle

One of our applications failed certification today because of an issue I should have known about. Even though the scenario in which an exception occurs is fairly easy, solving the problem properly involves a little work and a little thinking. Other blog entries have been written about this particular problem, although suggested solutions did not seem to work for me. Let me first show the original code:

MessageBox and Navigation
  1. private void TurnCard_Click(object sender, System.Windows.RoutedEventArgs e)
  2. {
  3.     MessageBoxResult mr = MessageBoxResult.Cancel;
  4.  
  5.     if (nrTimesClicked > 2)
  6.     {
  7.         mr = MessageBox.Show("Continuing now might confuse you. Try again tomorrow?",
  8.             "Continuing tomorrow?", MessageBoxButton.OKCancel);
  9.     }
  10.  
  11.     if (mr == MessageBoxResult.OK)
  12.     {
  13.         NavigationService.GoBack();
  14.     }
  15.     else
  16.     {
  17.         Random r = new Random();
  18.         int card = r.Next(cardPages.Length);
  19.         NavigationService.Navigate(new Uri(cardPages[card], UriKind.Relative));
  20.         nrTimesClicked++;
  21.     }
  22. }

Running this code and clicking the Start button on the phone while the MessageBox is visible results in the following exception when running with the debugger attached (it results in an application termination when running stand-alone):

image

The NavigationFailed event is raised. This is caused by the fact that the MessageBox returns when clicking the phone’s Start button with a return value of MessageBoxResult.Cancel. Since in the above code fragment, the Cancel button is used to navigate to some page, even though the application is supposed to go to the background in order to display the start screen on the phone, navigation fails. The interesting part is that the application does not have a clue that the user pressed the Start button on the phone. So some thinking is required to fix this issue.

Single stepping through the problematic method TurnCard_Click showed another behavior:

image

This at least made clear that indeed the call to the NavigationService caused the problem. To solve the problem, what can be done is protecting calls to the NavigationService by catching this particular exception:

Protecting the Navigate method
  1. private void TurnCard_Click(object sender, System.Windows.RoutedEventArgs e)
  2. {
  3.     MessageBoxResult mr = MessageBoxResult.Cancel;
  4.  
  5.     if (nrTimesClicked > 2)
  6.     {
  7.         mr = MessageBox.Show("Continuing now might confuse you. Try again tomorrow?",
  8.             "Continuing tomorrow?", MessageBoxButton.OKCancel);
  9.     }
  10.  
  11.     if (mr == MessageBoxResult.OK)
  12.     {
  13.         NavigationService.GoBack();
  14.     }
  15.     else
  16.     {
  17.         Random r = new Random();
  18.         int card = r.Next(cardPages.Length);
  19.         try
  20.         {
  21.             NavigationService.Navigate(new Uri(cardPages[card], UriKind.Relative));
  22.         }
  23.         catch (InvalidOperationException)
  24.         {
  25.             ;
  26.         }
  27.         nrTimesClicked++;
  28.     }
  29. }

This does solve the problem, although I don’t like it that an exception is thrown in a situation that the operating system should somehow prevent us against (maybe by returning some other value when the MessageBox returns without the user clicking on one of its buttons). If you want to have a little more flexibility, and don’t mind coding a bit more, there is another possible solution. Instead of using a MessageBox, you can make use of the Guide.BeginShowMessageBox method that is defined in the XNA Framework. This method is a bit more complex, but also much more flexible then its Silverlight counterpart. BeginShowMessageBox is asynchronous, which means that you need to take care with calling code that is supposed to run on the UI Thread.

Using BeginShowMessageBox
  1. private void TurnCard_Click(object sender, System.Windows.RoutedEventArgs e)
  2. {
  3.     if (nrTimesClicked > 2)
  4.     {
  5.         ShowContinueMessage();
  6.     }
  7.     else
  8.     {
  9.         Random r = new Random();
  10.         int card = r.Next(cardPages.Length);
  11.         nrTimesClicked++;
  12.         NavigationService.Navigate(new Uri(cardPages[card], UriKind.Relative));
  13.     }
  14. }
  15.  
  16. private void ShowContinueMessage()
  17. {
  18.     List<string> mbOptions = new List<string>();
  19.     mbOptions.Add("OK");
  20.     mbOptions.Add("Cancel");
  21.     string msg = "Continuing now might confuse you. Try again tomorrow?";
  22.     string title = "Continuing tomorrow?";
  23.     Guide.BeginShowMessageBox(title, msg, mbOptions, 0, MessageBoxIcon.Alert, EnteredAnswer, null);
  24.     messageBoxVisible = true;
  25. }
  26.  
  27. private void EnteredAnswer(IAsyncResult ar)
  28. {
  29.     int? result = Guide.EndShowMessageBox(ar);
  30.  
  31.     if (result != null)
  32.     {
  33.         messageBoxVisible = false;
  34.     }
  35.  
  36.     if (result != null && result == 0)
  37.     {
  38.         this.Dispatcher.BeginInvoke(delegate
  39.         {
  40.             NavigationService.GoBack();
  41.         });
  42.     }
  43.     else if (result != null)
  44.     {
  45.         Random r = new Random();
  46.         int card = r.Next(cardPages.Length);
  47.         this.Dispatcher.BeginInvoke(delegate
  48.         {
  49.             NavigationService.Navigate(new Uri(cardPages[card], UriKind.Relative));
  50.         });
  51.     }
  52. }

The interesting code can be found in the EnteredAnswer method, where we retrieve the key the user entered in the message box. Since EndShowMessageBox returns a nullable integer, it has this nice way of using null in those cases where the user did not click any key (in other words, for instance when the message box is removed as a result of the application going to the background). So now we have a way of detecting if we can navigate (result has a value other then null). The sample code also sets a boolean variable messageBoxVisible, which is used in combination with PhoneApplicationPage.State to display the message box again when the user returns to the application. Hopefully this time the application will pass certification.

EvenTiles from Start to Finish–Part 14

In the previous episode of this series about how to develop a Windows Phone application from scratch we found out that special care must be taken when data must be passed between an application and its PeriodicTask. You learned how data can be protected against mutual access by using a Mutex object. You also learned that you cannot pass data directly from one to another, because the application and its PeriodicTask execute inside different processes.

In this episode of EvenTiles you will learn how you can make use of IsolatedStorage to pass data from the application to the PeriodicTask. Since we already created a separate project in the EvenTiles solution that is taking care of passing data, we can simply modify functionality in that project (EvenTilesComm) to use a file to pass data between the application and its PeriodicTask. Data protection against mutual access is already in place, so we can concentrate on file access. Hopefully the design decision in part 13 to make use of private methods that are called each time we access a public property inside the TileData class starts to make sense now.

What we want to achieve is the following:

  • From inside the application we can store a string containing content for the backside of a Secondary Tile in a file at any time
  • Our PeriodicTask executes approximately once per 30 minutes and it either displays the string passed by the application on the backside of the Secondary Tile or it displays a default string that is defined inside the PeriodicTask
  • The content of the backside of the Secondary Tile needs to toggle each time the PeriodicTask executes
  • If the EvenTiles user modifies the string to be displayed inside the Settings page, it will immediately be displayed on the Secondary Tile (if it exists)

In order to pass data between the application and the PeriodicTask we will extend the private retrieve / store methods inside the TileData class by adding a call to a couple of other private methods. Those new private methods will use a file in IsolatedStorage to read / write data from, meaning we can effectively pass data between the application and its PeriodicTask.

Properties to access Tile Data
  1. private static string RetrieveSecondaryBackContent()
  2. {
  3.     mtx.WaitOne();
  4.  
  5.     try
  6.     {
  7.         RetrieveTileContent();
  8.         return secondaryBackContent;
  9.     }
  10.     finally
  11.     {
  12.         mtx.ReleaseMutex();
  13.     }
  14. }
  15.  
  16. private static void StoreSecondaryBackContent(string content)
  17. {
  18.     mtx.WaitOne();
  19.  
  20.     try
  21.     {
  22.         secondaryBackContent = content;
  23.         PersistTileContent();
  24.     }
  25.     finally
  26.     {
  27.         mtx.ReleaseMutex();
  28.     }
  29. }

A new string to be displayed on the back side of a Secondary Tile will be stored by the application each time users modify their own tile text in the Settings Page of the EvenTiles application (something that will also be done initially when the application starts). The PeriodicTask simply retrieves that data (since now the data is persisted in a file this will work properly) and displays it on the back side of the Secondary Tile. Each time data needs to be stored a new file is created in IsolatedStorage or an existing file is overwritten. Each time data needs to be retrieved, we check if a file containing that data exists. There are small ways to optimize data access in the TileData class, because right now we simply read / write all file content when retrieving / storing single property values. However, this approach simplifies the code and the overhead with only two different variables is very small.

Using IsolatedStorage
  1. private const string contentFileName = "EvenTileContent.txt";
  2.  
  3. private static void PersistTileContent()
  4. {
  5.     using (var store = IsolatedStorageFile.GetUserStoreForApplication())
  6.     {
  7.         using (var contentStream = new StreamWriter(store.CreateFile(contentFileName)))
  8.         {
  9.             contentStream.WriteLine(showDefaultSecondaryBackContent.ToString());
  10.             contentStream.WriteLine(secondaryBackContent);
  11.         }
  12.     }
  13. }
  14.  
  15. private static void RetrieveTileContent()
  16. {
  17.     using (var store = IsolatedStorageFile.GetUserStoreForApplication())
  18.     {
  19.         if (store.FileExists(contentFileName))
  20.         {
  21.             using (var contentStream = new StreamReader(store.OpenFile(contentFileName, FileMode.Open)))
  22.             {
  23.                 showDefaultSecondaryBackContent = Convert.ToBoolean(contentStream.ReadLine());
  24.                 secondaryBackContent = contentStream.ReadToEnd();
  25.             }
  26.         }
  27.     }
  28. }

With the way we organized the functionality inside the TileData class in the previous episode of this development series, these are the only new methods necessary in order to pass data. When the user installs a Secondary Tile for the EvenTiles application and modifies its back string content through the Settings page, this is the result (changing the back content of the Secondary Tile every 30 minutes):

image

The following video shows EvenTiles in action with proper transfer of data between the application and its PeriodicTask.

Data transfer between an Application and a PeriodicTask through IsolatedStorage

To be able to experiment with this working implementation of EvenTiles, especially to understand how the application interacts with the PeriodicTask through a file in IsolatedStorage, the sample code is available for dowload here.

Right now we have the basic functionality of EvenTiles more or less ready, although the About Page still needs to get some content. That is something we will work on in the next part of EvenTiles. After that we will cover much more in the upcoming episodes of EvenTiles including but not limited to

  • using ads in the application
  • retrieving location information inside the application
  • taking pictures
  • modifying pictures
  • using alarms and notifications
  • using Visual Studio’s integrated Performance Analysis to find performance bottlenecks inside the application
  • submitting the application for certification

EvenTiles will continue soon so stay tuned for the next episode.

EvenTilesIf you want to see EvenTiles already in action on your Windows Phone, you can also install the latest version from Marketplace. Remember that this application is not meant to be extremely useful, although it contains similar functionality that “serious” applications have. Just go ahead and get your free copy of EvenTiles from Marketplace at this location: http://www.windowsphone.com/en-US/search?q=EvenTiles (or search on your phone for EvenTiles in the Marketplace application).

EvenTiles from Start to Finish–Part 13

In the previous episode of this series about how to develop a Windows Phone application from scratch we fixed some issues to properly start and renew a PeriodicTask. Now we are ready to find out how an application can communicate with its PeriodicTask.

In today’s EvenTiles episode, you will learn why you cannot simply use a variable to communicate between the application and the PeriodicTask. In my opinion that is important, because understanding why some things are not working makes you a better Windows Phone developer and decreases the chance of having hard to trace bugs inside your own applications. In order to separate the functionality to transfer data between the application and its PeriodicTask, we are going to create a new project inside the EvenTiles solution.

Dump1

The project will be of type Windows Phone Class Library and we will call it EvenTilesComm. In its initial version, this class library will contain one single class with the name TileData. It is used to store the text that the user can enter in the Settings Page of the EvenTiles application in order to allow the PeriodicTask to display this string on the back of the Secondary Tile once it is created. In order to vary the text on the back of the Secondary Tile, TileData will also contain a boolean variable indicating which string needs to be displayed on the Secondary Tile’s backside (either the string passed by the application or a default string that is defined inside the PeriodicTask).

screenTo be able to access TileData from within both the EvenTiles application and the PeriodicTask, it contains two static properties. A string property to store / retrieve the message that is defined through the Settings Page and a boolean property that defines which string needs to be displayed from within the PeriodicTask. The string property is always written by the application and read by the PeriodicTask. The boolean variable is both written by the application and the PeriodicTask but only read by the PeriodicTask. Because of the fact that the PeriodicTask and the application are running inside their own, separate process in different threads, and since they are running completely unsynchronized from each other, there is a possibility that they both want to access one of the properties of TileData at the same time. To prevent this potential dangerous situation (dangerous because it can leave the properties in an undefined state when one of the two different threads want to access them while the other is updating them), both properties are protected by a Mutex synchronization object.

NOTE: The following (initial) implementation of the TileData class will not work properly, but it is shown here to help you understand what it means that an application and its PeriodicTask belong to each other, but are completely separated from each other at the same time. You will have to wait a little while for a working solution, because that will be introduced in the next episode of EvenTiles.

Threadsafe Data Access
  1. public class TileData
  2. {
  3.     private static Mutex mtx = new Mutex(false, "MtxSync");
  4.  
  5.     private static string secondaryBackContent;
  6.     private static bool showDefaultSecondaryBackContent;
  7.  
  8.  
  9.     public static string SecondaryBackContent
  10.     {
  11.         get
  12.         {
  13.             return RetrieveSecondaryBackContent();
  14.         }
  15.         set
  16.         {
  17.             StoreSecondaryBackContent(value);
  18.         }
  19.     }
  20.  
  21.     public static bool ShowDefaultSecondaryBackContent
  22.     {
  23.         get
  24.         {
  25.             return RetrieveShowDefaultSecondaryBackContent();
  26.         }
  27.         set
  28.         {
  29.             StoreShowDefaultSecondaryBackContent(value);
  30.         }
  31.     }
  32.  
  33.     private static string RetrieveSecondaryBackContent()
  34.     {
  35.         mtx.WaitOne();
  36.  
  37.         try
  38.         {
  39.             return secondaryBackContent;
  40.         }
  41.         finally
  42.         {
  43.             mtx.ReleaseMutex();
  44.         }
  45.     }
  46.  
  47.     private static void StoreSecondaryBackContent(string content)
  48.     {
  49.         mtx.WaitOne();
  50.  
  51.         try
  52.         {
  53.             secondaryBackContent = content;
  54.         }
  55.         finally
  56.         {
  57.             mtx.ReleaseMutex();
  58.         }
  59.     }
  60.  
  61.     private static bool RetrieveShowDefaultSecondaryBackContent()
  62.     {
  63.         mtx.WaitOne();
  64.  
  65.         try
  66.         {
  67.             return showDefaultSecondaryBackContent;
  68.         }
  69.         finally
  70.         {
  71.             mtx.ReleaseMutex();
  72.         }
  73.     }
  74.  
  75.     private static void StoreShowDefaultSecondaryBackContent(bool showDefaultContent)
  76.     {
  77.         mtx.WaitOne();
  78.  
  79.         try
  80.         {
  81.             showDefaultSecondaryBackContent = showDefaultContent;
  82.         }
  83.         finally
  84.         {
  85.             mtx.ReleaseMutex();
  86.         }
  87.     }
  88. }

If you look at the source code of the TileData class, you can see that we are using a two properties through which we are calling a few private methods to actually store or retrieve data. You will also see that we define a single Mutex inside the TileData class. This Mutex has a name, meaning it can be used to synchronize threads over multiple processes. This is important, since EvenTiles and its PeriodicTask will execute in separate processes. If we take a look at one of the individual retrieval methods, you will see how the single Mutex that is defined in TileData is used.

Retrieving data
  1. private static string RetrieveSecondaryBackContent()
  2. {
  3.     mtx.WaitOne();
  4.  
  5.     try
  6.     {
  7.         return secondaryBackContent;
  8.     }
  9.     finally
  10.     {
  11.         mtx.ReleaseMutex();
  12.     }
  13. }

In this method, we access the variable secondaryBackContent only when the method WaitOne on our Mutex returns. When WaitOne returns, our method has exclusive access to the variable secondaryBackContent until we call the method ReleaseMutex on our Mutex. At that moment, another thread that is waiting for the same Mutex will be granted access. Every method that uses of a Mutex to get access to protected data must under all circumstances call ReleaseMutex when it is done. That is the reason why we are adding a try / finally block. The code in the finally block will execute, even when exceptions are thrown. In this way, we prevent our application from a potential deadlock situations (where multiple threads are waiting on each other to free up a synchronization object that is never being freed).

The way data is protected for EvenTiles works properly, which can be verified by debugging the application and deliberately skipping a call to ReleaseMutex from within the EvenTiles application (which is fully explained in the accompanying video as well). Doing so will result in the PeriodicTask waiting forever for the Mutex, because it will never be released by the application. This at least proves that both EvenTiles and its PeriodicTask are making use of the same Mutex that they both need to own before being able to access variables.

There is still a potential problem, because we are calling the WaitOne method without parameters, which means that we will wait indefinitely until the Mutex becomes available. In our simple example this is sufficient, but in a real application you most likely want to wait for a specific time and take action upon timeouts.

However, there is a way bigger problem with the current implementation of our newly created TileData class. We already saw that the Mutex is properly shared between EvenTiles and its PeriodicTask, but what about the public properties. The following screen dump shows you what is wrong with the TileData class right now. Even though the variable secondaryBackContent is set by the EvenTiles application, it seems that it is not initialized when the PeriodicTask retrieves the variable (both through the appropriate properties).

image image

Since our EvenTiles application and its PeriodicTask are running in separate processes, they are completely separated from each other, meaning that they get their own copies of variables they both want to access, even though these variables are defined in a separate project. This is a good thing (even though we have to solve the problem), because otherwise all variables used by every process on the phone would need a unique name. The following video shows EvenTiles in action in combination with its PeriodicTask. It shows how the Mutex works and how passing data fails.

Protecting variables against simultaneous access by multiple threads

To be able to experiment with this wrong implementation of EvenTiles, especially to understand how the application interacts with the PeriodicTask, the sample code is available for dowload here.

Instead of using ‘simple’ variables to pass data between EvenTiles and its PeriodicTask, we will have to make use of IsolatedStorage and have them share data through a file. That will be the topic of the next episode of EvenTiles.

EvenTiles from Start to Finish–Part 12

In the previous episode of this series about how to develop a Windows Phone application from scratch we talked about debugging a PeriodicTask. Before digging deeper into exchanging data between an application and its PeriodicTask, we need to talk a little more about scheduling the PeriodicTask and about expiration of a PeriodicTask.

Going back to part 10 of the EvenTiles series, the PeriodicTask was created inside the MainPage.xaml.cs file in a method called StartPeriodicAgent. This private method takes care of stopping a currently active agent before creating a new one. It also handles an exception that might be thrown when an attempt is made to start a new PeriodicTask while the user has disabled background processing for the EvenTiles application, which they can do from inside the Phone Settings.

However, after submitting EvenTiles to Marketplace, I noticed an exceptionally high crash count in the application (as you can see from my AppHub dashboard). Since I want to share all details about developing this application, but also about the application’s life cycle, it is only fair to share the number of crashes to date, a number I am not proud off.

image

For starters, the first version of EvenTiles called LaunchForTest in release mode which results in a thrown exception as shows up in the call stack of various crash incidents. Also, the application does not count on other applications making use of background agents on a particular device. To preserve memory, but mainly battery power on the device, the number of background agents that can run at the same time is limited. It varies depending on the hardware capabilities of the device and might differ between devices, but it is a small number and therefore the limit can easily be reached. When this limit is reached, a SchedulerServiceException will be thrown whenever you attempt to add a periodic task. Of course you much handle this exception to prevent your application against crashing, since you are adding your periodic task from inside your application.

So, to create a periodic task, the following code should be used (which is an extension to the code that we used in part 10 of the series):

Correctly adding the agent
  1. private void StartPeriodicAgent()
  2. {
  3.     RemovePeriodicAgent();
  4.  
  5.     var evenTilesPeriodicTask = new PeriodicTask(evenTilesPeriodicTaskName);
  6.     evenTilesPeriodicTask.Description = "EvenTilesPeriodicTask";
  7.  
  8.     try
  9.     {
  10.         ScheduledActionService.Add(evenTilesPeriodicTask);
  11. #if DEBUG_TASK
  12.         ScheduledActionService.LaunchForTest(evenTilesPeriodicTaskName, TimeSpan.FromSeconds(30));
  13. #endif
  14.         App.PeriodicTaskScheduled = true;
  15.     }
  16.     catch (InvalidOperationException ex)
  17.     {
  18.         if (ex.Message.Contains("BNS Error: The action is disabled"))
  19.         {
  20.             MessageBox.Show("Background agents for this application are disabled.");
  21.         }
  22.     }
  23.     catch (SchedulerServiceException)
  24.     {
  25.         MessageBox.Show("Can't schedule your background agent. There might be too many background agents enabled. You can disable background agents through the phone's settings application.");
  26.     }
  27. }

If you take a look at how a periodic task is created you will also see that a App.PeriodicTaskScheduled is set to true. This static property is maintained in the App.xaml.cs file, its value is stored / retrieved from the IsolatedStorageSettings (see part 5 of EvenTiles for more information).

The reason to add this boolean property is to find out if the application did schedule a PeriodicTask. If a PeriodicTask was added for the application, you must realize that the PeriodicTask will age and eventually it will expire unless it is rescheduled by the application. A PeriodicTask will expire if it has not been rescheduled for 14 days from the time it was scheduled. Since the application is responsible for adding a PeriodicTask, it means that the PeriodicTask will only continue to run if the application is activated at least once every two weeks and if the application makes sure to reschedule the PeriodicTask. The approach that EvenTiles takes right now is to check if at some time a PeriodicTask was scheduled. If so, it will be rescheduled as soon as the EvenTiles’ MainPage is created.

NOTE: We could have used another way to determine if a PeriodicTask was scheduled, for instance by verifying if a SecondaryTile is currently available on the StartScreen.

Making use of IsolatedStorageSettings, we will add logic to the app.xaml.cs file to properly store and retrieve the App.PeriodicTaskScheduled property when the application is started, activated, deactivated and terminated.

To begin, a new property and a new const string to identify the value in the IsoloatedStorageSettings are created in app.xaml.cs as follows:

  1. public const string keyPTScheduled = "K_PTS";
  2.  
  3. public static bool PeriodicTaskScheduled { get; set; }

In the constructor, the PeriodicTaskScheduled property is explicitly initialized to false. Finally, the value of PeriodicTaskScheduled is stored / retrieved in the various application life cycle events:

Store/Retrieve property value
  1. // Code to execute when the application is launching (eg, from Start)
  2. // This code will not execute when the application is reactivated
  3. private void Application_Launching(object sender, LaunchingEventArgs e)
  4. {
  5.     if (appSettings.Contains(keyPTScheduled))
  6.     {
  7.         PeriodicTaskScheduled = (bool)appSettings[keyPTScheduled];
  8.     }
  9.  
  10.     if (!appSettings.Contains(keyActSecBackContent))
  11.     {
  12.         appSettings[keyActSecBackContent] = DefaultSecBackContent;
  13.     }
  14.     ActualSecBackContent = (string)appSettings[keyActSecBackContent];
  15. }
  16.  
  17. // Code to execute when the application is activated (brought to foreground)
  18. // This code will not execute when the application is first launched
  19. private void Application_Activated(object sender, ActivatedEventArgs e)
  20. {
  21.     if (!e.IsApplicationInstancePreserved)
  22.     {
  23.         PeriodicTaskScheduled = (bool)appSettings[keyPTScheduled];
  24.         ActualSecBackContent = (string)appSettings[keyActSecBackContent];
  25.     }
  26. }
  27.  
  28. // Code to execute when the application is deactivated (sent to background)
  29. // This code will not execute when the application is closing
  30. private void Application_Deactivated(object sender, DeactivatedEventArgs e)
  31. {
  32.     appSettings[keyPTScheduled] = PeriodicTaskScheduled;
  33.     appSettings[keyActSecBackContent] = ActualSecBackContent;
  34. }
  35.  
  36. // Code to execute when the application is closing (eg, user hit Back)
  37. // This code will not execute when the application is deactivated
  38. private void Application_Closing(object sender, ClosingEventArgs e)
  39. {
  40.     appSettings[keyPTScheduled] = PeriodicTaskScheduled;
  41.     appSettings[keyActSecBackContent] = ActualSecBackContent;
  42. }

Finally, we will use this property inside the MainPage.xaml.cs file when the page is constructed to determine if we need to reschedule a PeriodicTask:

Rescheduling PeriodicTask?
  1. public MainPage()
  2. {
  3.     InitializeComponent();
  4.     if (App.PeriodicTaskScheduled)
  5.     {
  6.         StartPeriodicAgent();
  7.     }
  8. }

Each time the PeriodicTask is scheduled, the App.PeriodicTaskProperty is set to true and a Secondary Tile is created. If the Secondary Tile is removed, the App.PeriodicTaskProperty is set to false as well.

The following video shows you how to modify the existing EvenTiles project to improve the way the PeriodicTask is scheduled as well as the functionality needed to reschedule the PeriodicTask:

Rescheduling a PeriodicTask

If you want to take a look at the source code that we have available for EvenTiles so far, you can download the entire solution belonging to this episode:

Download EvenTiles Episode 12 Source Code

After downloading and unzipping the sample code, you can open the EvenTiles solution in Visual Studio 2010 Express for Windows Phone. You must have the Windows Phone SDK 7.1 installed on your development system to do so. If you have a developer unlocked phone you can deploy the application to your phone, if you don’t have a developer unlocked phone you can still experiment with this application inside the emulator.

EvenTilesIf you want to see EvenTiles already in action on your Windows Phone, you can also install the latest version from Marketplace. Remember that this application is not meant to be extremely useful, although it contains similar functionality that “serious” applications have. Just go ahead and get your free copy of EvenTiles from Marketplace at this location: http://www.windowsphone.com/en-US/search?q=EvenTiles (or search on your phone for EvenTiles in the Marketplace application).

When EvenTiles continues with part 13, you will learn how to exchange data between an application and its background agent.