473,218 Members | 1,474 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,218 software developers and data experts.

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 7279
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Kent | last post by:
The following is a greatly simplified example of what I would like to accomplish using the event/delegate features of .NET I have a class "NumGen" that produces a random set of numbers between 0...
3
by: jayderk | last post by:
Hello All, I am running in to a situation where the listbox is not refreshing for me. I am using a timer to cycle every second and call the timer_elapsed() event. in the time_elapsed event...
13
by: Jason Jacob | last post by:
To all, I have a GUI program (use c#), and I have create a Thread for loading some bulk data, I also arrange the GUI program like this: 1) load a form showing "Wait for loading..." etc 2) a...
2
by: Jeff Van Epps | last post by:
We've been unable to get events working going from C# to VJ++. We think that the C# component is being exposed properly as a ConnectionPoint, and the Advise() from the VJ++ side seems to be...
9
by: Christopher Weaver | last post by:
Can anyone tell me how I could iterate through a collection of controls on a form while assigning their event handlers to another identical collection of controls on the same form. So far,...
2
by: Tim::.. | last post by:
Can someone tell me why I get this error??? And how do I fix it?? Thanks Compiler Error Message: BC30408: Method 'Public Sub UploadData()' does not have the same signature as delegate...
6
by: Dave | last post by:
I have a service that has 6 different threads. Each thread has a timer on it that elapses at about the same time (if not the same time). When the timer elapses I am trying to log a message by...
4
by: Elliot | last post by:
How can I pass the value of variable text to t except declare text at class level? .......... string text = "text"; Timer a = new Timer(); a.Interval = 10000; a.Start(); a.Tick += new...
4
by: =?iso-8859-1?B?S2VyZW0gR/xtcvxrY/w=?= | last post by:
Hi, i have a main thread an another worker thread. The main Thread creates another thread and waits for the threads signal to continue the main thread. Everything works inside a ModalDialog and...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.