468,765 Members | 1,296 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,765 developers. It's quick & easy.

Problem with EventHandler delegate (Timer)

Hello all,

New to the groups, sorry if this the wrong forum/etiquette. I am coding
a c++ application that requires the use of a timer-triggered event
handler. I decided to use the timer provided in System::Timers::Timer.
My understanding of the next part is not very good, as my code may
reveal, but as I understand it, my application is "unmanaged C++",
whereas the timer extension from the system DLL is managed. Therefore I
needed to use the gcroot template to allow the inclusion of the
"managed" timer code.

NOTE: (1) extraneous code/includes have been left out for brevity
(2) I'm using Visual C++ Express Edition Beta

***************** CODE BEGINS **************
#include <vcclr.h>

#using <mscorlib.dll>
using namespace System;

#using <System.dll>
using namespace System::Timers;

class DataStream
{
public:
DataStream():m_N(48),m_saving(false)
{
// set up the window timer
m_Timer = gcnew Timer;
m_Timer->Elapsed += gcnew
ElapsedEventHandler(DataStream::nextCandle);
m_Timer->Interval= CANDLE_DURATION * 1000;
m_Timer->AutoReset= true;
m_Timer->Enabled=true;
}; // default constructor

private:
gcroot<Timer^> m_Timer; // use gcroot because can't use managed
object
in unmanaged class

void nextCandle(Object ^sender, ElapsedEventArgs ^e);

};

void DataStream::nextCandle(Object ^sender/*source*/, ElapsedEventArgs
^e/*e*/)
{
// do some stuff

}

**************CODE ENDS ************

Here's the problem, upon compilation, I get this error:
Compiling...
dataStream.cpp
c:\blah\dataStream.h(18) : error C3867: 'DataStream::nextCandle':
function call missing argument list; use '&DataStream::nextCandle' to
create a pointer to member
c:\blah\dataStream.h(18) : error C3350:
'System::Timers::ElapsedEventHandler' : a delegate constructor expects
2 argument(s)

At first, I didn't include the & reference suggested by the compiler
because most examples I had seen do not use this.

Upon inclusion, changing:

m_Timer->Elapsed += gcnew ElapsedEventHandler(DataStream::nextCandle);

to....

m_Timer->Elapsed += gcnew ElapsedEventHandler(&DataStream::nextCandle);

I get the following error on compilation:

c:\blah\dataStream.h(18) : error C3364:
'System::Timers::ElapsedEventHandler' : invalid argument for delegate
constructor; delegate target needs to be a pointer to a member function

So I'm stuck at this point. I'm not sure if the solution is a few small
changes away from where I am, or if these errors are indicative of a
larger problem (i.e. me using the gcroot template and mixing managed
and unmanaged code with zero experience in that).

Any help would be appreciated!!

Thank you!

Jan 9 '06 #1
2 7075
za********@gmail.com wrote:
Hello all,

New to the groups, sorry if this the wrong forum/etiquette. I am
coding a c++ application that requires the use of a timer-triggered
event handler. I decided to use the timer provided in
System::Timers::Timer. My understanding of the next part is not very
good, as my code may reveal, but as I understand it, my application
is "unmanaged C++", whereas the timer extension from the system DLL
is managed. Therefore I needed to use the gcroot template to allow
the inclusion of the "managed" timer code.


I'll let someone else tackle why your managed timer doesn't work, but...

If your application is unmanaged, you should probably use a Windows Timer
instead of dragging the entire CLR into your app just to get a timer.

See the function SetTimer in the Platform SDK for information about Windows
timers.

http://msdn.microsoft.com/library/de...s/settimer.asp

Note that if you're using VC++ Express and you haven't installed the
platform SDK, then you're very likely not doing unmanaged development.

-cd
Jan 9 '06 #2
Change this line:

m_Timer->Elapsed += gcnew ElapsedEventHandler(DataStream::nextCandle);

to this:

m_Timer->Elapsed += gcnew ElapsedEventHandler(this,
&DataStream::nextCandle);

The definition of the handler needs 2 arguments, the first one being the
source or location of the handler. I'm assuming it's in the same class,
hence the 'this'. The other problem was you needed to put a '&' in front of
the handler name to tell indicate it's an address (which is just a syntax
thing that was added, I still don't know why, except I'm told this is
compliant with C, which I find funny as an argument when defining that is
something non-standard).

Hope this helps...!

[==P==]
<za********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Hello all,

New to the groups, sorry if this the wrong forum/etiquette. I am coding
a c++ application that requires the use of a timer-triggered event
handler. I decided to use the timer provided in System::Timers::Timer.
My understanding of the next part is not very good, as my code may
reveal, but as I understand it, my application is "unmanaged C++",
whereas the timer extension from the system DLL is managed. Therefore I
needed to use the gcroot template to allow the inclusion of the
"managed" timer code.

NOTE: (1) extraneous code/includes have been left out for brevity
(2) I'm using Visual C++ Express Edition Beta

***************** CODE BEGINS **************
#include <vcclr.h>

#using <mscorlib.dll>
using namespace System;

#using <System.dll>
using namespace System::Timers;

class DataStream
{
public:
DataStream():m_N(48),m_saving(false)
{
// set up the window timer
m_Timer = gcnew Timer;
m_Timer->Elapsed += gcnew
ElapsedEventHandler(DataStream::nextCandle);
m_Timer->Interval= CANDLE_DURATION * 1000;
m_Timer->AutoReset= true;
m_Timer->Enabled=true;
}; // default constructor

private:
gcroot<Timer^> m_Timer; // use gcroot because can't use managed
object
in unmanaged class

void nextCandle(Object ^sender, ElapsedEventArgs ^e);

};

void DataStream::nextCandle(Object ^sender/*source*/, ElapsedEventArgs
^e/*e*/)
{
// do some stuff

}

**************CODE ENDS ************

Here's the problem, upon compilation, I get this error:
Compiling...
dataStream.cpp
c:\blah\dataStream.h(18) : error C3867: 'DataStream::nextCandle':
function call missing argument list; use '&DataStream::nextCandle' to
create a pointer to member
c:\blah\dataStream.h(18) : error C3350:
'System::Timers::ElapsedEventHandler' : a delegate constructor expects
2 argument(s)

At first, I didn't include the & reference suggested by the compiler
because most examples I had seen do not use this.

Upon inclusion, changing:

m_Timer->Elapsed += gcnew ElapsedEventHandler(DataStream::nextCandle);

to....

m_Timer->Elapsed += gcnew ElapsedEventHandler(&DataStream::nextCandle);

I get the following error on compilation:

c:\blah\dataStream.h(18) : error C3364:
'System::Timers::ElapsedEventHandler' : invalid argument for delegate
constructor; delegate target needs to be a pointer to a member function

So I'm stuck at this point. I'm not sure if the solution is a few small
changes away from where I am, or if these errors are indicative of a
larger problem (i.e. me using the gcroot template and mixing managed
and unmanaged code with zero experience in that).

Any help would be appreciated!!

Thank you!

Jan 9 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Kent | last post: by
3 posts views Thread by jayderk | last post: by
13 posts views Thread by Jason Jacob | last post: by
2 posts views Thread by Jeff Van Epps | last post: by
9 posts views Thread by Christopher Weaver | last post: by
2 posts views Thread by Tim::.. | last post: by
4 posts views Thread by Elliot | last post: by
4 posts views Thread by =?iso-8859-1?B?S2VyZW0gR/xtcvxrY/w=?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.