473,800 Members | 2,499 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
ElapsedEventHan dler(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(Obje ct ^sender, ElapsedEventArg s ^e);

};

void DataStream::nex tCandle(Object ^sender/*source*/, ElapsedEventArg s
^e/*e*/)
{
// do some stuff

}

**************C ODE ENDS ************

Here's the problem, upon compilation, I get this error:
Compiling...
dataStream.cpp
c:\blah\dataStr eam.h(18) : error C3867: 'DataStream::ne xtCandle':
function call missing argument list; use '&DataStream::n extCandle' to
create a pointer to member
c:\blah\dataStr eam.h(18) : error C3350:
'System::Timers ::ElapsedEventH andler' : 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 ElapsedEventHan dler(DataStream ::nextCandle);

to....

m_Timer->Elapsed += gcnew ElapsedEventHan dler(&DataStrea m::nextCandle);

I get the following error on compilation:

c:\blah\dataStr eam.h(18) : error C3364:
'System::Timers ::ElapsedEventH andler' : 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 7317
za********@gmai l.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 ElapsedEventHan dler(DataStream ::nextCandle);

to this:

m_Timer->Elapsed += gcnew ElapsedEventHan dler(this,
&DataStream::ne xtCandle);

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********@gma il.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.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
ElapsedEventHan dler(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(Obje ct ^sender, ElapsedEventArg s ^e);

};

void DataStream::nex tCandle(Object ^sender/*source*/, ElapsedEventArg s
^e/*e*/)
{
// do some stuff

}

**************C ODE ENDS ************

Here's the problem, upon compilation, I get this error:
Compiling...
dataStream.cpp
c:\blah\dataStr eam.h(18) : error C3867: 'DataStream::ne xtCandle':
function call missing argument list; use '&DataStream::n extCandle' to
create a pointer to member
c:\blah\dataStr eam.h(18) : error C3350:
'System::Timers ::ElapsedEventH andler' : 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 ElapsedEventHan dler(DataStream ::nextCandle);

to....

m_Timer->Elapsed += gcnew ElapsedEventHan dler(&DataStrea m::nextCandle);

I get the following error on compilation:

c:\blah\dataStr eam.h(18) : error C3364:
'System::Timers ::ElapsedEventH andler' : 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
1291
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 and 1000. Each time a number is generated an event called "NewNumber" is raised. I have another class "NumReceiver" with anywhere between 25 and 50 instances where each instance is interested in only a certain number produced by NumGen. Some...
3
1974
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 Method I have code that checks to see if a DB table has been updated.. if so the new new data is uploaded to the listbox by calling lstbox1.datasource = myDBDataSet;
13
3638
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 Thread is then created to load the bulk data 3) after the thread has completed, close the "Wait for loading" form 4) show the main form for the GUI program
2
1738
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 working, because the delegate on the C# side has 1 item in its invocation list after the Advise(). However when we try to make the callback, we get: System.NullReferenceException: Object reference not set to an instance of an object at...
9
3092
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, thanks to another programmer, I've got this working out quite nicely for the properties: Type ctrlType = subject.GetType(); ConstructorInfo cInfo = ctrlType.GetConstructor(Type.EmptyTypes); Control retControl = (Control)cInfo.Invoke(null);
2
1316
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 'Delegate Sub EventHandler(sender As Object, e As System.EventArgs)'.
6
4224
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 writing to a text file. The problem is that not every thread is able to log its message. I thought it was because each thread is trying to use the same resource. So i put in mutex.waitone() and released the mutex at the end of the method. It...
4
2816
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 EventHandler(t); ..........
4
5721
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 everyting is secured by Invoke/BeginInvoke, and synchronisation primitves like WaitHandles, Evetns, Semaphores, etc... All works good and with no race conditions or locks. I have a System.Windows.Forms.Timer in the main
0
9690
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10504
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10274
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10033
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9085
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6811
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5469
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4149
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.