locked
How to use DispatcherTimer class with C++?

    Question

  • I can ref new a DispatcherTimer object in my metro CPP project,but I have no idea how to fire it up?

    and how to declare the TimeSpan,Tick and Interval? any demo with CPP?

    Wednesday, December 07, 2011 6:22 PM

Answers


  • using namespace Windows::UI::Xaml;
    using namespace Windows::Foundation;
    
    
    void Application1::MainPage::Button_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
    {
    	DispatcherTimer^ timer = ref new DispatcherTimer;
    	timer->Tick += ref new Windows::UI::Xaml::EventHandler(this, &Application1::MainPage::DispatcherTimer_Tick);
    	TimeSpan t;
    	t.Duration=1000;
    	timer->Interval = t;
    	timer->Start();
    }
    
    void Application1::MainPage::DispatcherTimer_Tick(Platform::Object^ sender, Platform::Object^ e)
    {
       // TO DO...
    }
    

    Just declare the DispatcherTimer, and register one event handler on it.


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us

    • Edited by Bob_Bao Thursday, December 08, 2011 7:02 AM
    • Marked as answer by Stephen Yang.L Friday, December 09, 2011 5:26 AM
    Thursday, December 08, 2011 7:00 AM
  • The sample code in this thread is from the Developer Preview. The EventHandler type moved to Windows::Foundation. 

            dispatcherTimer->Tick += ref new Windows::Foundation::EventHandler<Object^>(this, &Application1::DispatcherTimer_Tick); 
    
    --Rob
       
    Thursday, August 16, 2012 10:26 PM
    Owner

All replies


  • using namespace Windows::UI::Xaml;
    using namespace Windows::Foundation;
    
    
    void Application1::MainPage::Button_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
    {
    	DispatcherTimer^ timer = ref new DispatcherTimer;
    	timer->Tick += ref new Windows::UI::Xaml::EventHandler(this, &Application1::MainPage::DispatcherTimer_Tick);
    	TimeSpan t;
    	t.Duration=1000;
    	timer->Interval = t;
    	timer->Start();
    }
    
    void Application1::MainPage::DispatcherTimer_Tick(Platform::Object^ sender, Platform::Object^ e)
    {
       // TO DO...
    }
    

    Just declare the DispatcherTimer, and register one event handler on it.


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us

    • Edited by Bob_Bao Thursday, December 08, 2011 7:02 AM
    • Marked as answer by Stephen Yang.L Friday, December 09, 2011 5:26 AM
    Thursday, December 08, 2011 7:00 AM
  • In Windows 8 Metro

    timer->Tick += ref new Windows::UI::Xaml::EventHandler(this, &Application1::MainPage::DispatcherTimer_Tick);

    The Windows::UI::Xaml::EventHandler generated an error: expected a type specifier.

    And timer->Tick += timer->Tick generated an add error.


    Charlie Chang L

    Thursday, August 16, 2012 9:57 PM
  • The sample code in this thread is from the Developer Preview. The EventHandler type moved to Windows::Foundation. 

            dispatcherTimer->Tick += ref new Windows::Foundation::EventHandler<Object^>(this, &Application1::DispatcherTimer_Tick); 
    
    --Rob
       
    Thursday, August 16, 2012 10:26 PM
    Owner
  • Great. That DispatcherTimer += worked. But

    timer->Tick += timer->Tick

    worked in C# generates a syntax error in C++. Any solution?


    Charlie Chang L

    Friday, August 17, 2012 4:00 PM
  • What are you trying to do with timer->Tick += timer->Tick? That doesn't look like it should work in either C# or C++.

    To set a Tick event handler in C++ use the syntax I provided. The syntax in C# is slightly different.

    --Rob

    Friday, August 17, 2012 10:22 PM
    Owner
  • It is my mistake. In C# it should be:

    timer.Tick += DispatcherTimer_Tick;

    timer->Tick += ref new Windows::Foundation::EventHandler<Object^>(this, &Application1::DispatcherTimer_Tick);

    But in C++ below it is an error (without ref new).

    timer->Tick += DispatcherTimer_Tick;


    Charlie Chang L

    Saturday, August 18, 2012 3:16 AM
  • C# and C++ are different languages. You cannot use C# syntax in a C++ app.

    --Rob

    Saturday, August 18, 2012 5:11 AM
    Owner