locked
Variation while debugging and deploying the app.? RRS feed

  • Question

  • Hi, 

    I have created an app which has a button. Button_Click is the event handler for the button. If we click the button I am setting a timer call back for 10 sec. As of my understanding this call back will be executed from a background thread. So even if I suspend the app, or press home screen, the call back will happen. In the callback I am creating a notification in the action center. This works fine when I am in the visual studio debugger(ie I ran the app , clicked the button, suspended the app using visual studio suspend option then after 10 sec the notification was created ). But when I deploy the app and press home screen, the notification is not created. Can anyone suggest the reason for this...? And possibly a way to accomplish this ...

    Code is as follows:

     private Timer stateTimer;
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                TimerCallback timerDelegate = new TimerCallback(timer_Tick);
                TimeSpan delayTime = new TimeSpan(0, 0, 10);
                AutoResetEvent autoEvent = new AutoResetEvent(false);
                TimeSpan intervalTime = new TimeSpan(0, 0, 0, 0, 0);
                Timer notification_timer = new Timer(timerDelegate, autoEvent, delayTime, intervalTime);
            }
    
            private void timer_Tick(object state)
            {
                ToastTemplateType toastTemplate = ToastTemplateType.ToastText02;
                XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(toastTemplate);
    
                XmlNodeList toastTextElements = toastXml.GetElementsByTagName("text");
                toastTextElements[0].AppendChild(toastXml.CreateTextNode("Download has complete"));
                toastTextElements[1].AppendChild(toastXml.CreateTextNode("Download has complete"));
    
                IXmlNode toastNode = toastXml.SelectSingleNode("/toast");
                ((XmlElement)toastNode).SetAttribute("duration", "long");
    
                ((XmlElement)toastNode).SetAttribute("launch", "{\"type\":\"toast\",\"param1\":\"12345\",\"param2\":\"67890\"}");
                ToastNotification toast = new ToastNotification(toastXml);
                ToastNotificationManager.CreateToastNotifier().Show(toast);
            }

    Monday, February 9, 2015 4:12 PM

Answers

  • The Timer will always execute in the context of the App. So if the App is suspended before the Timer triggers it won't trigger while you are on the homescreen. Attaching the Debugger affects suspension behavior (and quite a few other things like Memory Limit, etc.)

    If you want to trigger notifications at a specific time in the background you would have to use a BackgroundTask in conjunction with a TimeTrigger.

    You can find more information on that in this article: https://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh977059.aspx

    Given that you are showing that a Download has completed - regular Downloads happening in the main App will also be suspended when the user navigates away from it. If you want to have a Download continue while your App is not active you will have to use the BackgroundDownloader, which is described here: https://msdn.microsoft.com/en-US/library/windows/apps/hh452979.aspx

    Tuesday, February 10, 2015 7:20 AM