none
VC++ Display RRS feed

  • Question

  • In C++

    I have callback associated with a Button Push which works fine, I also set a System::Timer

    that basically calls the Button Push callback directly. And the strange part is only one item on the

    window paints even though its being processed I never see the data on my window. The "this"

    pointer seems fine and I get no errors. And even more strange is when I was in debug mode

    and several timer event call backs piled up when I finally let the program continue the screen painted

    once

    Thursday, July 18, 2013 1:52 PM

Answers

  • Hi,

    The following code example sets up an event handler for the Timer::Elapsed event, creates a timer, and starts the timer. The event handler displays the SignalTime property each time it is raised.

    #using <system.dll>
    using namespace System;
    using namespace System::Timers;
    public ref class Timer1
    {
    private: 
       static System::Timers::Timer^ aTimer;
    public:
       static void Demo()
       {
          // Normally, the timer is declared at the class level, 
          // so that it stays in scope as long as it is needed. 
          // If the timer is declared in a long-running method,   
          // KeepAlive must be used to prevent the JIT compiler  
          // from allowing aggressive garbage collection to occur  
          // before the method ends. You can experiment with this 
          // by commenting out the class-level declaration and  
          // uncommenting the declaration below; then uncomment 
          // the GC::KeepAlive(aTimer) at the end of the method. 
          //System::Timers::Timer^ aTimer; 
          // Create a new Timer with Interval set to 10 seconds.
          aTimer = gcnew System::Timers::Timer( 10000 );
          // Hook up the Elapsed event for the timer.
          aTimer->Elapsed += gcnew ElapsedEventHandler( Timer1::OnTimedEvent );
          // Set the Interval to 2 seconds (2000 milliseconds).
          aTimer->Interval = 2000;
          aTimer->Enabled = true;
          Console::WriteLine("Press the Enter key to exit the program.");
          Console::ReadLine();
          // If the timer is declared in a long-running method, use 
          // KeepAlive to prevent garbage collection from occurring 
          // before the method ends. 
          //GC::KeepAlive(aTimer);
       }
    private:
       // Specify what you want to happen when the Elapsed event is  
       // raised. 
       static void OnTimedEvent( Object^ source, ElapsedEventArgs^ e )
       {
          Console::WriteLine( "The Elapsed event was raised at {0}", e->SignalTime );
       }
    };
    int main()
    {
       Timer1::Demo();
    }

    Hope it can help you.



    Sunny Cao
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Anna Cc Tuesday, July 30, 2013 1:40 AM
    Thursday, July 25, 2013 7:35 AM

All replies

  • Hi, can you give me some more information about your System::Timer.
    Friday, July 19, 2013 4:55 AM

  • This is how I set up the timer


                        check_admin = gcnew System::Timers::Timer;
                        check_admin->Elapsed  += gcnew ElapsedEventHandler(this,&ValveAdmin::Form1::call_get_admin);
                        check_admin->Interval = 10000;
                        check_admin->Enabled  = true;

    This is the callback

    public:  System::Void call_get_admin(System::Object^ obj, System::Timers::ElapsedEventArgs^ e)
             {           
                GetStatusBtn_Click(obj, e);
             }

    private: System::Void GetStatusBtn_Click(System::Object^  sender, System::EventArgs^  e)
    {
        //this is a sample of the things I am trying to do in callback
        this->insidePktIn->Text = gcnew String(psingle->stat._n_io_rcv);
    }

    Things I was wondering about;
    1) the "this" pointer (I checked it and it is the same as when I do the button click myself)
    2) the event type is different

    What I saw;
    1st time the event is fired one component in the window paints (the 1st one i tried to change)
    when I stopped at a break point and checked a bunch of stuff more timer events queued and
    when I finally did a continue I got one full refresh that seemed right. I think because is a timer
    event arg I passed in the window does not know to refresh. I was looking for a way to force
    a refresh
    Friday, July 19, 2013 1:56 PM
  • Do you run your program in vs 2012?

    Is your project a MFC or Win32?

    Tuesday, July 23, 2013 3:09 AM
  • Hi,

    The following code example sets up an event handler for the Timer::Elapsed event, creates a timer, and starts the timer. The event handler displays the SignalTime property each time it is raised.

    #using <system.dll>
    using namespace System;
    using namespace System::Timers;
    public ref class Timer1
    {
    private: 
       static System::Timers::Timer^ aTimer;
    public:
       static void Demo()
       {
          // Normally, the timer is declared at the class level, 
          // so that it stays in scope as long as it is needed. 
          // If the timer is declared in a long-running method,   
          // KeepAlive must be used to prevent the JIT compiler  
          // from allowing aggressive garbage collection to occur  
          // before the method ends. You can experiment with this 
          // by commenting out the class-level declaration and  
          // uncommenting the declaration below; then uncomment 
          // the GC::KeepAlive(aTimer) at the end of the method. 
          //System::Timers::Timer^ aTimer; 
          // Create a new Timer with Interval set to 10 seconds.
          aTimer = gcnew System::Timers::Timer( 10000 );
          // Hook up the Elapsed event for the timer.
          aTimer->Elapsed += gcnew ElapsedEventHandler( Timer1::OnTimedEvent );
          // Set the Interval to 2 seconds (2000 milliseconds).
          aTimer->Interval = 2000;
          aTimer->Enabled = true;
          Console::WriteLine("Press the Enter key to exit the program.");
          Console::ReadLine();
          // If the timer is declared in a long-running method, use 
          // KeepAlive to prevent garbage collection from occurring 
          // before the method ends. 
          //GC::KeepAlive(aTimer);
       }
    private:
       // Specify what you want to happen when the Elapsed event is  
       // raised. 
       static void OnTimedEvent( Object^ source, ElapsedEventArgs^ e )
       {
          Console::WriteLine( "The Elapsed event was raised at {0}", e->SignalTime );
       }
    };
    int main()
    {
       Timer1::Demo();
    }

    Hope it can help you.



    Sunny Cao
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Anna Cc Tuesday, July 30, 2013 1:40 AM
    Thursday, July 25, 2013 7:35 AM