Background thread loading through code

May 29, 2012 at 8:21 PM

I add the ad control (no pun intended) to my apps after the main app and all data has been downloaded.  I do this to speed up the initial app load and display, but also because I think it looks nicer.  I also had a ton of problems with the pubcenter control throwing exceptions in prior versions so I wrapped it into an ad handler class that inserts the control in a named panel when I want it, and then always removed it prior to navigation events.

I just so happen to be adding the control through a thread (not a background worker).

I have noticed that some of the ad controls work fine (Pubcenter), but some of them crash with invalid access.

AdMob.AdMobAdProvider.ProvideAd() throws an exception (UnauthorizedAccessException) during the display.

I tried to add a dispatch to ensure it happens on the UI thread, but it still gives me the error.

Any idea how I could accomplish this?

Dispatcher.BeginInvoke(() =>
	if (!hasAdControl)
		hasAdControl = true;
		AdHandler.InsertAdControl(AdPanel, AdPage.ItemDetailPage);
May 29, 2012 at 8:37 PM

Hi Jason,

First of all I understand that you have wrapped the PubCenter control to add/remove it during navigation events. This is also handled by the UnifiedAd itself, so it's no longer needed. I can't see what your implementation of AdHandler.InsertAdcontrol looks like, but I've for example added the AdControl in the Loaded event. If you can provide a stacktrace that would certainly help, but even better a small example that I can easily run. 

Hope you can provide a little bit more information so I can reproduce this.


Mark Monster

May 29, 2012 at 10:10 PM

Thanks for the quick response!

Here is the stacktrace.

Short Stacktrace

0 at Microsoft.Phone.Info.ExtendedPropertiesHelper.GetValue(String propertyName, GetValueImplDelegate getValueImpl)
1 at Microsoft.Phone.Info.DeviceExtendedProperties.GetValue(String propertyName)
2 at MC.Phone.Ads.AdMob.AdMobAdProvider.ProvideAd()
3 at MC.Phone.Ads.AdControl.b__b(IAdProvider provider)
4 at Microsoft.Phone.Reactive.AnonymousObserver`1.Next(IAdProvider value)
5 at Microsoft.Phone.Reactive.AbstractObserver`1.OnNext(IAdProvider value)


If you already handle the ad insert and remove during navigation then I can probably remove most of the code I have around that.  But how would you handle setting up things like the remote ad and isTest mode dynamically for debug / release?  That is the only thing I can't seem to figure out.

I usually leave the adproviders to isTest=true during debug builds.

I would actually prefer to have the control settings in my app resources (ad providers, remote url, etc).  But then just place the actual control with a template target on each page.


May 29, 2012 at 10:26 PM

As a quick test I got rid of all my code and just hard coded the adcontrol on each page for this app.  The problem went away.  So there must be something in the way I am doing it.  Will have to dig further.

My only real complaint with putting it on the page is that I can't set things like the pubcenter ad number, istest, remoteurl, etc through configuration.  I don't want to have to rememeber to hit a bunch of pages every time those things change.

May 30, 2012 at 7:54 AM

Hi Jason,


There are still options to fill those settings from code, either by code-behind or databinding.

Code Behind

If you give the elements (AdControl and/or AdProviders) a name by using x:Name you can access them easily in your code behind.

Data Binding

You can easily databind the different properties, either by adding the information to a ViewModel object and using expressions like {Binding PropName}  instead of the value in the Xaml. Or you add everything as Resources in for example the App.xaml and bind like {StaticResource PropName}.


Hope this helps,

Mark Monster

May 30, 2012 at 11:30 PM

It would be nice if you had an example of putting the values in a resource dictionary at the app level and then referencing it in the app.  I think that would be a better approach for most apps.


Jun 4, 2012 at 8:46 PM

A small example. You can add the below code to your App.xaml Resources section.


        <System:Boolean x:Key="IsTest">true</System:Boolean>
        <System:String x:Key="AdDuplexAdProviderApp">aabb/System:String>


And then reference it in your xaml like this:

<AdDuplex:AdDuplexAdProvider App="{StaticResource AdDuplexAdProviderApp}" IsTest="{StaticResource IsTest}"/>

Hope this helps,

Mark Monster

Jun 4, 2012 at 10:49 PM

Yes, that was exactly what I needed.  Took me a while to figure out that I had to add the System reference in the app.xaml because the String and Boolean types kept coming back as undefined.

Of course I would still like to do a conditional, #if DEBUG around then to set values based upon the compile time setting.  But you can't do that in XAML.

I did add them off the App object with DEBUG conditional logic and got it running that way.  Thanks for the help.