More on mobile ads on Windows Phone 8 (and file locking)

I continue to have two issues with my little Software Serial Manager app: ads not showing, and an issue with file-locking.

Windows Phone ads:
First, with my latest release I found that it was not showing ads either some or all of the time. I ended up talking with Microsoft about this ad stuff (you can start a chat session by clicking here).

In short, they said that ads are not always guaranteed to show. The user may not have a cell data signal or Wi-Fi or it could be that they just don’t have ads in your area that fit the criteria. They pointed me to this blog post:

In there, he describes a strategy for having multiple ad layers – and if an ad fails, one of the other ad providers will hopefully show an ad. As a sure-fire fail-safe, he has a static ad at the bottom of the “stack”. So, if all of the ad services fail, the end-user will see something – which is typically a self or cross-promotion link that goes directly to your website. You can think of it like this:


If Microsoft PubCenter refreshes and has an ad, I show it. If not, I hide that ad control. Meanwhile, underneath, the Google ad control (I used AdMob) may already have an ad loaded. When it refreshes, same thing, I show it if there is an ad, hide it if it doesn’t.

In the event that MS AND Google don’t have ads, then I have a static image that is clickable to my website – which is always there, but just not typically visible because the ad controls on top of it, hide it. I did this by putting all of the ad stuff in a canvas, and then using the z-index to specify which layer is on top of which:


Because Microsoft doesn’t allow you to see ads while debugging, they want you to use a test id. So, instead of setting the adunit here in markup, I do that in the OnNavigatedTo event handler with something like this:


So, if it’s a debug build (like, in Visual Studio), then I see a mock advertisement, but in production on a real phone, I should see real ads. That is, in theory – because I can’t test it, except to push it out as a release!

Ok, lastly, here’s what those event handlers look like from above. I start off by showing my static ad, then the Google ad, and then the Microsoft ad. Then, as the ads refresh, if they do work or if they fail, I show or hide that ad control appropriately as ad updates come in (every 30-60 seconds):


This seems to work in limited testing that I’ve done. I’ll push this out with the next release and see how it works on a real phone.

File Locking:
Let me first explain why I’m annoyed. When you force someone into a multi-threaded environment, you MUST provide mechanisms for thread synchronization and locking. In Windows Phone, everything is async/await. This means that operations kick off in another thread and the UI is freed up to be responsive in the meantime.

Although that is fine, what if I NEED things to run synchronously or what if I need to synchronize access to a resource? Well, as discussed, I created a poor-mans-lock-keyword. That fixed the original problem, but also created a deadlock. Meaning, the app – instead of crashing, would now hang, waiting for the lock to be freed. So, I went from one extreme to the other!

When I dug into this in Visual Studio, sure enough, I saw that multiple calls to .Load() were being called, and because my ViewModel is a singleton, it created a deadlock.

So, I added bad-code after bad-code to fix that problem. I have a Boolean flag to keep track of if I am already in the .Load() method, and if I am, then exit out. This seems to fix the problem. So now, this ugly code looks like this:


Then in my try..finally, I set that to true and false, respectively. In my tests in Visual Studio, this seemed to both load the data on all screens and not create any locks.

To be clear on what I am trying to do: several parts of the screen either want to .Load() or Save() the current ViewModel to make sure it’s in-sync with what is on the file system. However, since these requests can come from arbitrary places, I need a way to synchronize those requests so that ANYone can call Load or Save and the program won’t blow up.

I REALLY wish there was a proper way to do this. When they force us to use the async/await keywords AND won’t let us use any existing synchronization methods (like the lock{..} keyword), it’s frustrating.

Again, this seems to fix all the known problems, so this will ship with the next release.

Posted in Ads, Uncategorized, WindowsPhone

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Enter your email address to follow this blog and receive notifications of new posts by email.

Join 2 other followers

%d bloggers like this: