Download Binary and CodeThe .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.
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
Pin the App to the Start ScreenMake 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.
Write Iso StorageTo 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.LastTimeFileTouched=1/1/0001 12:00:00 AM
WWW-data.LastTimeFileTouched=11/14/2011 8:52:23 AM
Update TileLet’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.
Verify Settings | Applications | Background tasksLet’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.
Go back to AppNow, hold down the back button and select the sample app.
So far, the app touched mutexed isolated storage and the tile. The agent hasn’t been started.
Start AgentThe 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.
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 TestSelect 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.
The output window showed the Agent’s OnInvoke() call, as well as exiting the code at the end.
RRR-data.LastTimeFileTouched=11/14/2011 9:07:38 AM
WWW-data.LastTimeFileTouched=11/14/2011 9:09:34 AM
RRR-data.LastTimeFileTouched=11/14/2011 9:09:34 AM
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 ' 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 AgentMove 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.
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 CyclingThe 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.