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
Printable View
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
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.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;
}
Have fun!
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.
Cool Swift and Hi 5 Guys,
Thanks to Darwen and NoHero..... !!
A tip means a million hopes.
:-D
Hilarious.. i am looking into the wrong timer.. for use... gosh..
Actually the timer function which i plan to use is the System::Timers..
anyone got any idea on this sample from the msdn? i just cant get it to work
Quote:
[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
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.
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..
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. :D
Darwen.
true darwen and i agree on that.
Even documentation can't help you nowhere sometimes..