A Simple WP7 Mango App for Background Tasks, Toast, and Tiles

Download Binary and Code

The .XAP binary and code solution are both available for download. The app uses a background process that does some work then updates the tile and pops up toast. The app and the agent talk to each other via an isolated storage file protected by a named mutex. The diagram below shows the pieces and how they work together.

This is the app associated with this post.

App/Agent Architecture


clip_image001

The app and agent don’t actually talk to a web service as this is a sample but in a real world solution they both would. The app would do the heavy lifting. The agent would have a quick, light conversation with just enough information to update the tile and pop up toast with meaningful information.

In this sample, you can configure the agent to run repetitively every minute, second, etc. This is usually done for debug/testing. In the usual release situation, the agent runs on the 30 minute cycle.

Start the App

clip_image006

Pin the App to the Start Screen

Make sure to the pin the app to the start screen as that is where the toast and tile changes will appear. Notice no toast or tile changes yet.
clip_image007

Write Iso Storage

To begin, select the “Write Iso Storage” button, which sets the iso storage to have the name of the process that touched it (“App”) and the datetime the storage was written to. You can verify the iso storage data by looking at the UI which reads from the iso storage after the write.

In this sample, I also write to the output windows to show this. This information in the output window helps me keep track of when and who wrote to isolated storage. RRR means reading from storage. WWW means writing to storage.


RRR-data.LastProcessToTouchFile=
RRR-data.LastTimeFileTouched=1/1/0001 12:00:00 AM
RRR-data.CycleAgentEveryMinute=False
WWW-data.LastProcessToTouchFile=App
WWW-data.LastTimeFileTouched=11/14/2011 8:52:23 AM
WWW-data.CycleAgentEveryMinute=False


Update Tile

Let’s update the app tile to show the iso storage data by selecting the “Update Tile” button then select the phone’s start button to go to the start screen and verify the tile has been updated with “App” and a time. The count in the circle at the top of the tile should be 1. It’s an arbitrary number for this sample. “1” is another signifier meaning the app set the tile. “2” means the agent set the tile.
clip_image008

Verify Settings | Applications | Background tasks

Let’s verify that, while the app is running, the background process for this app has not been turned on yet. Select the start button and go to settings| applications | background tasks. The app of “SampleApp1” should not be in the list.

clip_image009

Go back to App

Now, hold down the back button and select the sample app.

clip_image010
So far, the app touched mutexed isolated storage and the tile. The agent hasn’t been started.

Start Agent

The next thing we can do is select the “Start Agent” button. The background agent does three things: 1) updates the iso storage, 2) pops up toast, and 3) updates the existing tile. Notice the app UI hasn’t changed. If you select the “Read Iso Storage” button, you still see no changes.

And a final place to look, notice that the output windows doesn’t show any work from the background agent. The point is that starting the background agent didn’t do any obvious work. Let’s verify the code did something. Go back to settings | application | background tasks.

clip_image011

Great! The app is now in the background task list so it is “on” but hasn’t run yet. By default, this app uses the standard 30 minute cycle for the agent. But we don’t have to wait that long. Go back to the app.

Launch For Test

Select the “Launch For Test” button and then pop back out to the phone’s start screen. Also make sure you can see the output window of Visual Studio. You may have to wait a few seconds to a minute or more, depending on what your phone is doing at the time.

The start screen showed a change to the tile as well as toast popped up.
clip_image012

The output window showed the Agent’s OnInvoke() call, as well as exiting the code at the end.

SampleAgent.SampleAgent.OnInvoke
RRR-data.LastProcessToTouchFile=Agent
RRR-data.LastTimeFileTouched=11/14/2011 9:07:38 AM
RRR-data.CycleAgentEveryMinute=False
WWW-data.LastProcessToTouchFile=Agent
WWW-data.LastTimeFileTouched=11/14/2011 9:09:34 AM
WWW-data.CycleAgentEveryMinute=False
RRR-data.LastProcessToTouchFile=Agent
RRR-data.LastTimeFileTouched=11/14/2011 9:09:34 AM
RRR-data.CycleAgentEveryMinute=False
The thread '<No Name>' (0xe12012a) has exited with code 0 (0x0).
The thread '<No Name>' (0xe270136) has exited with code 0 (0x0).
The thread '<No Name>' (0xfb30142) has exited with code 0 (0x0).
The program '[253296902] Background Task: Managed' has exited with code 0 (0x0).

If you continue waiting, nothing more will happen as the launch was a one-time event. However, we can change that.

Continuous 1min Cycle of Agent

Move back to the app. The text info at the top of the UI should show the same thing the start screen tile and output window showed.

clip_image013

When the agent is started, it is on a 30 minute cycle. When we launch for test, we run the agent once immediately. But if we want the agent to run repeatedly, we need to configure the agent to do that. This sample app does that via the “Continuous 1min Cycle of Agent” checkbox.

Go ahead and check it. The checkbox writes to iso storage the new setting and the agent reads the setting. At the bottom of the OnInvoke() of the agent, the launch is repeated.

Continuous Cycling

The UI changes to say the app touched storage. Now select the “Launch For Test” button again to get the cycle started. Go back to the start screen. Keep an eye on the tile, toast, and Visual Studio output window. The tile and toast should update and the output window should report agent begin and ending. This cycle should repeat until you stop the uncheck the box, stop the agent, stop the debug session, or uninstall the app.

Caution: If you deployed or debugged on your physical phone, instead of the emulator, remember to stop the agent and delete the sample app when you are done. Otherwise, this can be an annoying app with the constant toast pop ups.

The Code

I’ll explain the code in the next blog post.

Comments

  1. I was stuck trying to use ApplicationSettings for sharing which wasn't working, this is a great example, thanks.

    ReplyDelete

Post a Comment

Popular posts from this blog

Yet once more into the breech (of altered programming logic)

Simple WP7 Mango App for Background Tasks, Toast, and Tiles: Code Explanation

How to convert SVG data to a Png Image file Using InkScape