locked
Cancel a ThreadPoolTimer in windows store apps

    Question

  • Hello everybody !!

    I have this code : 

      var dispatcher = CoreApplication.MainView.CoreWindow.Dispatcher;
                    var period = TimeSpan.FromSeconds(2);
                    var PeriodicTimer = ThreadPoolTimer.CreatePeriodicTimer(async (source) =>
                        {
                            //TODO
    
                            await dispatcher.RunAsync(CoreDispatcherPriority.High,
                              () =>
                              {
                                  //TODO : A décommenter
                                  //if (responseEtatCreationDataBase.Terminer == true)
                                  if (responseEtatCreationDataBase==true)
                                  {
                                      
                                      //TODO : à décommenter
    
                                  }
    
                              });
                           
    
                        }, period);

    I need to stop this Periodic Timer when the responseEtatCreationDatabase = true.

    Somebody may help me please?

    Please do not send me an url, i need the right code to finish this step

    Thanx


    • Edited by DiddyRennes Monday, January 5, 2015 10:25 AM
    Monday, January 5, 2015 10:22 AM

Answers

  • XAML:
    <StackPanel>
      <TextBox Width="500" Height="600" AcceptsReturn="True" x:Name="Log"/>
      <Button Content="Cancel Timer" Click="Button_Click"/>
    </StackPanel>

    Your code slightly modified:
    bool responseEtatCreationDataBase = false;
    ThreadPoolTimer PeriodicTimer;
    
            protected override void OnNavigatedTo(NavigationEventArgs e)
            {
                
                var dispatcher = CoreApplication.MainView.CoreWindow.Dispatcher;
                var period = TimeSpan.FromSeconds(2);
                PeriodicTimer = ThreadPoolTimer.CreatePeriodicTimer(async (source) =>
                {
                    //TODO
    
                    await dispatcher.RunAsync(CoreDispatcherPriority.High,
                      () =>
                      {
                          //TODO : A décommenter
                          //if (responseEtatCreationDataBase.Terminer == true)
                          Log.Text += "Ran at " + DateTime.Now.ToString() + "\r\n";
                          if (responseEtatCreationDataBase == true)
                          {
                              Log.Text += "Timer cancelled";
                              PeriodicTimer.Cancel();
                          }
    
                      });
                }, period);
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                responseEtatCreationDataBase = true;
            }


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.


    Monday, January 5, 2015 3:17 PM
    Moderator

All replies

  • XAML:
    <StackPanel>
      <TextBox Width="500" Height="600" AcceptsReturn="True" x:Name="Log"/>
      <Button Content="Cancel Timer" Click="Button_Click"/>
    </StackPanel>

    Your code slightly modified:
    bool responseEtatCreationDataBase = false;
    ThreadPoolTimer PeriodicTimer;
    
            protected override void OnNavigatedTo(NavigationEventArgs e)
            {
                
                var dispatcher = CoreApplication.MainView.CoreWindow.Dispatcher;
                var period = TimeSpan.FromSeconds(2);
                PeriodicTimer = ThreadPoolTimer.CreatePeriodicTimer(async (source) =>
                {
                    //TODO
    
                    await dispatcher.RunAsync(CoreDispatcherPriority.High,
                      () =>
                      {
                          //TODO : A décommenter
                          //if (responseEtatCreationDataBase.Terminer == true)
                          Log.Text += "Ran at " + DateTime.Now.ToString() + "\r\n";
                          if (responseEtatCreationDataBase == true)
                          {
                              Log.Text += "Timer cancelled";
                              PeriodicTimer.Cancel();
                          }
    
                      });
                }, period);
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                responseEtatCreationDataBase = true;
            }


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.


    Monday, January 5, 2015 3:17 PM
    Moderator
  • Hello Matt !...Thanx for your answer?

    In fact I have this code : 

     //Affichage des états de création 
                              await dispatcher.RunAsync(CoreDispatcherPriority.High,
                                 async () =>
                                 {
    
                                     EtatCreationDataBaseResponse responseEtatCreationDataBaseTemp = await _middlewareService.GetEtatCreationDatabase(FileName);
    
                                     if (responseEtatCreationDataBaseTemp.Terminer == true)
                                     {
                                         //Téléchargement de la base de données en cours + arrêt de du timer pour l'exécution de la requête toutes les 20 secondes
                                         EtatAvancement = responseEtatCreationDataBaseTemp.EtatCreation;
    
                                      // I NEED TO STOP THE PERIODIC TIMER HERE !! 
                                     }
                                 });
    Thanx very much !


    MCTS : Web Applications Development with Microsoft .NET Framework 4 / Accessing Data with Microsoft .NET Framework 4 / Pro: Designing and Developing Web Applications Using Microsoft .NET Framework 4.0

    Monday, January 5, 2015 4:57 PM
  • This is the magic:

    PeriodicTimer.Cancel();

    Does that make sense?


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Monday, January 5, 2015 6:00 PM
    Moderator