CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: Timer Core?

  1. #1
    Join Date
    Feb 2005
    Posts
    64

    Talking Timer Core?

    Guys,

    Can anyone lead me to any timer for c++ .net ?
    I hope to have the timer in my project to control the timing .. Thank You

  2. #2
    Join Date
    Mar 2004
    Location
    (Upper-) Austria
    Posts
    2,899

    Re: Timer Core?

    Code:
    // Timer example
    //
    
    #pragma managed
    
    #using <mscorlib.dll>
    #using <System.dll>
    #using <System.Windows.Forms.dll>
    
    using namespace System;
    using namespace System::Windows::Forms;
    
    __gc class MainForm : public Form
    {
    private:
    
    	Timer * m_Timer;
    	Label * m_Label;
    
    public:
    
    	MainForm ( void )
    	{
    		// Initialize the main window a bit
    		//
    		this->Width = 500;
    		this->Height = 300;
    		this->Text = S"Timer example";
    		// And a label
    		//
    		m_Label = new Label();
    		m_Label->Height = 20;
    		m_Label->Width = 200;
    		m_Label->Text = S"Timer callback";
    		
    		this->Controls->Add(m_Label);
    		// Create new timer
    		//
    		m_Timer = new Timer();
    		
    		// 1 Second
    		//
    		m_Timer->Interval = 1000;
    		// Add Event handler
    		//
    		m_Timer->add_Tick(new EventHandler(this, OnTimer));
    		// Start the timer
    		//
    		m_Timer->Enabled = true;
    		m_Timer->Start();
    	}
    
    	~MainForm ( void )
    	{
    		// Stop it
    		//
    		m_Timer->Stop();
    		m_Timer->Enabled = false;
    	}
    
    protected:
    
    	virtual void OnTimer ( Object *sender, EventArgs* args )
    	{
    		static Int64 times = 0;
    
    		times++;
    		m_Label->Text = String::Format("Event happened {0} times!", __box(times));
    	}
    
    };
    
    // Main entry point
    //
    int main ( void )
    {
    	Application::Run(new MainForm());
    	return 0;
    }
    This shows how to use System::Windows::Forms::Timer. I don't know any other timer for .NET, but you may find any thirdparty library which can help you creating a better timer than this. You don't have to use a Form to use this Timer, this above is just an example to demonstrate the System::Windows::Forms::Timer usage.

    Have fun!
    I am not offering technical guidiance via email or IM
    Come on share your photo with us! CG members photo album!
    Use the Code Tags!

  3. #3
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: Timer Core?

    There's several timer classes available to .NET.

    System::Windows::Forms::Timer - uses message passing on the main UI thread.
    System::Threading::Timer - invokes the timer procedure in a seperate thread.
    System::Timers::Timer - meant for servers.

    Have a look here :

    http://msdn.microsoft.com/library/de...asedTimers.asp

    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  4. #4
    Join Date
    Feb 2005
    Posts
    64

    Resolved Re: Timer Core?

    Cool Swift and Hi 5 Guys,

    Thanks to Darwen and NoHero..... !!
    A tip means a million hopes.

    :-D
    Last edited by codegurugeek; September 3rd, 2005 at 12:56 AM.

  5. #5
    Join Date
    Feb 2005
    Posts
    64

    Lightbulb Re: Timer Core?

    Hilarious.. i am looking into the wrong timer.. for use... gosh..

    Actually the timer function which i plan to use is the System::Timers..

  6. #6
    Join Date
    Feb 2005
    Posts
    64

    Exclamation Re: Timer Core?

    anyone got any idea on this sample from the msdn? i just cant get it to work

    [C++]
    public __gc class Timer1
    {
    public:
    static void Main() {
    System::Timers::Timer* aTimer = new System::Timers::Timer;
    aTimer->Elapsed += new ElapsedEventHandler(0, Timer1::OnTimedEvent);
    // Set the Interval to 5 seconds.
    aTimer->Interval=5000;
    aTimer->Enabled=true;
    }
    private:
    // Specify what you want to happen when the Elapsed event is raised.
    static void OnTimedEvent(Object* /*source*/, ElapsedEventArgs* /*e*/)
    {
    Console::WriteLine(S"Hello World!");
    }
    };

    int main()
    {
    Timer1::Main();

    Console::WriteLine(S"Press \'q\' to quit the sample.");
    while(Console::Read()!='q');
    }




    error ::
    StopWatchTimer.cpp(33) : error C2724: 'Timer1::OnTimedEvent' : 'static' should not be used on member functions defined at file scope

  7. #7
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: Timer Core?

    To have OnTimedEvent static, you have to define the method in a .cpp file (I believe - it must be a .NET-ism because this is fine in native C++).

    Just remove the "static" and all should be well.

    Also remove Timer1::OnTimedEvent and just put OnTimedEvent.

    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  8. #8
    Join Date
    Feb 2005
    Posts
    64

    Thumbs up Re: Timer Core?

    Thank You Darwen... You save the day of my hazard again.. Appreciated

    Sometimes.. the documentation is also not updated and drive developer like us crazy figuring out the non-existance solution..

  9. #9
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: Timer Core?

    To be honest, I wouldn't use VC++ to write anything more than a little .NET if you paid me. It's too much like banging your head against a brick wall for my liking.

    It's ok to do managed wrappers for native code of course, but this has problems as I've said before.

    It seems what's happening, certainly from what I can draw from this forum, is what I suspected might happen : that newbie C++ people are trying to learn C++/CLR and getting hideously confused between managed and native code. You really need to understand what's going on behind the scenes to be able to make best use of it. And to do that you have to know native C++ before mixing up the issues with C++/CLR.

    And I hate the syntax.

    These days I stick to C# for managed and C++ for native : writing COM interfaces where necessary.

    I'll get off my soapbox now.

    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  10. #10
    Join Date
    Feb 2005
    Posts
    64

    Resolved Re: Timer Core?

    true darwen and i agree on that.

    Even documentation can't help you nowhere sometimes..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured