0

I have a simple app that I am creating with a countdown timer that uses a DispatcherTimer for the time base. I have an event handler setup for On_Tick of the DispatcherTimer (set for 1 sec interval). I have three (3) pivot pages using three different instances of AdControl and all are "live" with a real ApplicationID and AdUnitID. This timer is setup on one of the pivot pages.

What I am seeing is that when I open my app and the AdControl starts, after 60 seconds, the adControl wants to refresh. My timer works fine for the first minute, then starts to lose a second every three seconds, like it is missing a tick event (coincidentally when the adcontrol "scrolls" to a new message every three seconds?). I've tried using a background worker for the dispatcherTimer but that did not seem to do anything for me. The code in the event handler is fairly short, with just a couple of "if-then" statements and a few textBlock updates.

Anyone else seen similar issues with the AdControl?

ctacke
  • 66,480
  • 18
  • 94
  • 155

2 Answers2

0

I've experienced the same problem with my own timer style app. In my case it only appears to happen when there is animation in the current advertisement.

According to the DispatcherTimer documentation, the delay is expected behaviour, so the solution it to use a different timer... eg System.Threading.Timer

...
    //create the timer
    var timer = new System.Threading.Timer(
        new System.Threading.TimerCallback(TimerTick), 
        null, 
        //Set the due time to infinite so the timer wont start immediately
        System.Threading.Timeout.Infinite,
        0);

    //start the timer
    timer.Change(0, 1000);

    //stop the timer
    timer.Change(System.Threading.Timeout.Infinite, 0);
}

void TimerTick(object state)
{
    //Dont forget to update the UI on the UI thread.
    Dispatcher.BeginInvoke(() =>
        {
            MyTextBox.Text = "New Text";
        });
}

Problem solved!

Silver Solver
  • 2,310
  • 1
  • 13
  • 19
0

I would say the reason is that the ad control and the timer both want to do something on the UI thread. Thus when the ad control is busy the timer action is blocked during this time. To quote MSDN:

Timers are not guaranteed to execute exactly when the time interval occurs, but they are guaranteed to not execute before the time interval occurs. This is because DispatcherTimer operations are placed on the Dispatcher queue like other operations. When the DispatcherTimer operation executes is dependent on the other jobs in the queue and their priorities.

It also explains why using a background worker does not help. As soon as you go back from another thread to the UI thread you have the same problem again. So this issue is basically by design.

Oh and it can also be the other way round. If you would be doing intensive work in the UI thread then the ad control would be blocked. As well as the rest of your UI. This is the reason why you should do as much work as possible in background threads. Maybe the ad control doesn't adhere to this advice.

So far this probably won't help you much. But perhaps it is possible to just use one AdControl and move this from Pivot to Pivot as the user pans around?

Heinrich Ulbricht
  • 10,064
  • 4
  • 54
  • 85