473,811 Members | 2,859 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to fire async events and c++/clr

Greetings,

I am trying to fire aysnc events in c++/clr, and I can't seem to get it to
work.
It seems to work fine in c#, but when I try c++/clr, I can't seem to get it
to compile in the c++/clr class.

I.) Event Testing in C#, which works fine:
public class CSharpEventTest
{
public event CSharpDelegate AsyncEvent = null;
public event CSharpDelegate SyncEvent = null;

public void FireAsyncEvent( string message)
{
if (AsyncEvent == null) return;

AsyncEvent.Begi nInvoke(message , null, this);
}

public void FireSyncEvent(s tring message)
{
if (SyncEvent == null) return;

SyncEvent(messa ge);
}

}
II. Event Testing in MC++, which doesn't compile correctly.
public delegate bool MCDelegate(Syst em::String^ errorCode);

public ref class MCEventTesting
{
public:
MCEventTesting( void);

event MCDelegate^ EventSync;
event MCDelegate^ EventASync;

public:
void FireEventAsync( System::String^ errorCode)
{
// Doesn't compile, gives an error:
//"invalid argument for delegate constructor; delegate target needs to be a
// pointer to a member function"
MyErrorDelegate ^ delg = gcnew MyErrorDelegate (EventASync(err orCode));
delg.BeginInvok e(NULL, delg);
}

// Works fine
void FireEventSync(S ystem::String^ errorCode)
{
EventSync(error Code);
}
};

Thanks for any suggestions!
Nov 6 '06 #1
9 5743
BartMan wrote:
public:
void FireEventAsync( System::String^ errorCode)
{
// Doesn't compile, gives an error:
//"invalid argument for delegate constructor; delegate target needs to be a
// pointer to a member function"
MyErrorDelegate ^ delg = gcnew MyErrorDelegate (EventASync(err orCode));
MyErrorDelegate ^ delg = gcnew MyErrorDelegate (this,
&MCEventTesting ::EventASync);

The first argument (this) is a handle to the handler class. The second
argument (&MCEventTestin g::EventASync) is a pointer to the handler function.

Tom
Nov 6 '06 #2
Hello Tamas,

Thanks for the reply. I tried your recommendation for the method.
MyErrorDelegate ^ delg = gcnew MyErrorDelegate (this,
&MCEventTesting ::EventASync);
But I get the following error:
Error 1 error C3364: invalid argument for delegate constructor; delegate
target needs to be a pointer to a member function

For some reason the "event MCDelegate^ EventASync;" is not working as a
member function.

Any ideas?


"Tamas Demjen" wrote:
BartMan wrote:
public:
void FireEventAsync( System::String^ errorCode)
{
// Doesn't compile, gives an error:
//"invalid argument for delegate constructor; delegate target needs to be a
// pointer to a member function"
MyErrorDelegate ^ delg = gcnew MyErrorDelegate (EventASync(err orCode));

MyErrorDelegate ^ delg = gcnew MyErrorDelegate (this,
&MCEventTesting ::EventASync);

The first argument (this) is a handle to the handler class. The second
argument (&MCEventTestin g::EventASync) is a pointer to the handler function.

Tom
Nov 7 '06 #3
BartMan wrote:
But I get the following error:
Error 1 error C3364: invalid argument for delegate constructor; delegate
target needs to be a pointer to a member function
You didn't disclose the declaration of MyErrorDelegate , but it looks
like it's not compatible with the handler. Your event handler must have
the exact same arguments and return type as the delegate. For example,
MCDelegate is declared

public delegate bool MCDelegate(Syst em::String^ errorCode);

So the event handler must be declared

bool xxx(System::Str ing^ errorCode);

Tom
Nov 7 '06 #4
You didn't disclose the declaration of MyErrorDelegate , but it looks
like it's not compatible with the handler. Your event handler must have
the exact same arguments and return type as the delegate. For example,
MCDelegate is declared
Opps a typo on my part the delegate was declared, and the event is declared
below, and it has the same values, so I don't understand how it cannot
compile.
Especially since the c# works fine?

// Here is the Delegate:
public delegate bool MCDelegate(Syst em::String^ errorCode);

public ref class MCEventTesting
{
public:
MCEventTesting( void);

event MCDelegate^ SyncEvent;
event MCDelegate^ AsyncEvent; // Async Event?????

public:
// Fire the event as an async event.
void fireErrorEventA sync(System::St ring^ errorCode)
{
// ERROR: "Complains that it must be a pointer to a member function."
MCDelegate^ delg = gcnew MCDelegate(this ,&MCEventTestin g::AsyncEvent);
AsyncEvent->BeginInvoke("t est",nullptr,th is);

// Also tried this, it didn't work complained that it needs to be a data
member.
// MCEventTesting: :AsyncEvent->BeginInvoke(er rorCode,nullptr ,nullptr);
}

// Works fine.
void fireSyncEvent(S ystem::String^ errorCode)
{
SyncEvent(error Code);
}
};
Nov 7 '06 #5

"BartMan" <Ma******@commu nity.nospamwrot e in message
news:9F******** *************** ***********@mic rosoft.com...
>You didn't disclose the declaration of MyErrorDelegate , but it looks
like it's not compatible with the handler. Your event handler must have
the exact same arguments and return type as the delegate. For example,
MCDelegate is declared

Opps a typo on my part the delegate was declared, and the event is
declared
below, and it has the same values, so I don't understand how it cannot
compile.
Especially since the c# works fine?

// Here is the Delegate:
public delegate bool MCDelegate(Syst em::String^ errorCode);

public ref class MCEventTesting
{
public:
MCEventTesting( void);

event MCDelegate^ SyncEvent;
event MCDelegate^ AsyncEvent; // Async Event?????

public:
// Fire the event as an async event.
void fireErrorEventA sync(System::St ring^ errorCode)
{
// ERROR: "Complains that it must be a pointer to a member function."
MCDelegate^ delg = gcnew MCDelegate(this ,&MCEventTestin g::AsyncEvent);
AsyncEvent->BeginInvoke("t est",nullptr,th is);

// Also tried this, it didn't work complained that it needs to be a data
member.
// MCEventTesting: :AsyncEvent->BeginInvoke(er rorCode,nullptr ,nullptr);
Use this along with a custom event:

private:
MCDelegate^ AsyncHandlerLis t;

public:
event MCDelegate^ Async {
MCDelegate^ add(MCDelegate^ d) { return AsyncHandlerLis t =
Delegate::Combi ne(AsyncHandler List, d); }
MCDelegate^ remove(MCDelega te^ d) { return AsyncHandlerLis t =
Delegate::Remov e(AsyncHandlerL ist, d); }
}

void fireErrorEventA sync(System::St ring^ errorCode)
MCEventTesting: :AsyncHandlerLi st->BeginInvoke(er rorCode,nullptr ,nullptr);
}
>
// Works fine.
void fireSyncEvent(S ystem::String^ errorCode)
{
SyncEvent(error Code);
}
};

Nov 7 '06 #6
event MCDelegate^ Async {
MCDelegate^ add(MCDelegate^ d) { return AsyncHandlerLis t =
Delegate::Combi ne(AsyncHandler List, d); }
MCDelegate^ remove(MCDelega te^ d) { return AsyncHandlerLis t =
Delegate::Remov e(AsyncHandlerL ist, d); }
}
Hello Ben,

Thanks for the reply. The above mentioned code issues the following error:
Error 1 error C3919: 'Async::add': function must have type 'void (MCDelegate
^)'

I tried changing it to "void add(MCDelegate^ d) {....}", but that didn't
seem to work either.

Nov 7 '06 #7

"BartMan" <Ma******@commu nity.nospamwrot e in message
news:A7******** *************** ***********@mic rosoft.com...
>event MCDelegate^ Async {
MCDelegate^ add(MCDelegate^ d) { return AsyncHandlerLis t =
Delegate::Comb ine(AsyncHandle rList, d); }
MCDelegate^ remove(MCDelega te^ d) { return AsyncHandlerLis t =
Delegate::Remo ve(AsyncHandler List, d); }
}
Hello Ben,

Thanks for the reply. The above mentioned code issues the following
error:
Error 1 error C3919: 'Async::add': function must have type 'void
(MCDelegate
^)'

I tried changing it to "void add(MCDelegate^ d) {....}", but that didn't
seem to work either.
also take out the word return. Oh, and the += and -= operators do work in
C++. Here's a snippet out of my own code:

virtual event System::EventHa ndler^ Closed

{

void add( System::EventHa ndler^ handler )

{

CloseHandlers += handler;

}

void remove( System::EventHa ndler^ handler )

{

CloseHandlers += handler;

}

}
Nov 7 '06 #8
also take out the word return. Oh, and the += and -= operators do work in
C++. Here's a snippet out of my own code:

virtual event System::EventHa ndler^ Closed

{

void add( System::EventHa ndler^ handler )

{

CloseHandlers += handler;

}

void remove( System::EventHa ndler^ handler )

{

CloseHandlers += handler;

}

}
Thanks that is very good to know. I did not know that operator support has
been added in c++/clr. Thank for all your help, it is exactly what I needed.

Here is my final version which seems to execute properly.

public delegate bool MCDelegate(Syst em::String^ errorCode);

public ref class MCEventTesting
{
public:
MCEventTesting( void);

event MCDelegate^ SyncEvent;

private:
MCDelegate^ AsyncHandlerLis t;

public:
event MCDelegate^ Async
{

void add(MCDelegate^ d)
{
AsyncHandlerLis t =
(MCDelegate^)Sy stem::Delegate: :Combine(AsyncH andlerList, d);
}

void remove(MCDelega te^ d)
{
AsyncHandlerLis t = (MCDelegate^)Sy stem::Delegate: :Remove(AsyncHa ndlerList,
d);
}
}

public:
void fireErrorEventA sync(System::St ring^ errorCode)
{
MCEventTesting: :AsyncHandlerLi st->BeginInvoke(er rorCode,nullptr ,nullptr);
}

void fireSyncEvent(S ystem::String^ errorCode)
{
SyncEvent(error Code);
}
};
Nov 7 '06 #9
"BartMan" <Ma******@commu nity.nospamwrot e in message
news:34******** *************** ***********@mic rosoft.com...
Thanks that is very good to know. I did not know that operator support
has
been added in c++/clr. Thank for all your help, it is exactly what I
needed.

Here is my final version which seems to execute properly.

public delegate bool MCDelegate(Syst em::String^ errorCode);

public ref class MCEventTesting
{
public:
MCEventTesting( void);

event MCDelegate^ SyncEvent;

private:
MCDelegate^ AsyncHandlerLis t;

public:
event MCDelegate^ Async
{

void add(MCDelegate^ d)
{
AsyncHandlerLis t =
(MCDelegate^)Sy stem::Delegate: :Combine(AsyncH andlerList, d);
As I showed you with my code, this can be shortened to: AsyncHandlerLis t +=
d
}

void remove(MCDelega te^ d)
{
AsyncHandlerLis t = (MCDelegate^)Sy stem::Delegate: :Remove(AsyncHa ndlerList,
d);
And here, AsyncHandlerLis t -= d
}
}

public:
void fireErrorEventA sync(System::St ring^ errorCode)
{
MCEventTesting: :AsyncHandlerLi st->BeginInvoke(er rorCode,nullptr ,nullptr);
Don't forget to get the IAsyncResult and call EndInvoke later. And yes,
this is a pain, but it's required. You can pass an anonymous method as the
callback to BeginInvoke to help with this.
}

void fireSyncEvent(S ystem::String^ errorCode)
{
SyncEvent(error Code);
}
};

Nov 7 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
10460
by: Vanessa | last post by:
I have a question regarding async mode for calling Microsoft.XMLHTTP object. Microsoft.XMLHTTP hangs the IE once in a while suddenly, but it will work again after half an hour or so without doing anything. I have searched through the Internet and seems like the reason it hangs the browser it's because XMLHTTP limits you to two concurrent HTTP connections to each remote host; so if more than 2 concurrent connections strike the script...
8
3523
by: MuZZy | last post by:
Hi, Could someone pls help me here: If i use async sockets in the separate thread like this: void ThreadFunction() { ..... 1. MySocket.BeginAccept(AsyncCallBack(OnConnectRequest), MySocket);
10
2967
by: Shawn Meyer | last post by:
Hello - I am trying to write a class that has an async BeginX and EndX, plus the regular X syncronous method. Delegates seemed like the way to go, however, I still am having problems getting exactly what I want. Here are my goals 1. I would like the IAsyncResult that is returned by the Begin function to be able to be waited on or polled to check for completion. 2. The Begin function would take a callback, and the async process would
2
1190
by: Mark Broadbent | last post by:
I initially posted a problem in the aspnet.webcontrols and have now found why the problem existed. Just thought I'd post this to see if this was perhaps a known issue. Scenario: I have a simple webform project that was created on one machine and transported to my work machine. The form has two buttons Edit and Add which do not cause validation. When either of them are clicked they fire their respective events and cause textboxes (and...
5
4200
by: Stephen Barrett | last post by:
I have read many threads related to async fire and forget type calls, but none have addressed my particular problem. I have a webpage that instantiates a BL object and makes a method call. The BL object method actually sets up a delegate and calls a private method asynchronously and immediately returns back to the web page. The problem I am having is that the async call never happens. I added a quick logging call immediately as the...
4
5298
by: Ty Salistean | last post by:
So, here is a wierd question that we have been discussing for a bit now. Does an event fire even though nothing is subscribed to listen to the event? For instance, does the Click event of a button fire even though nothing is subscribed to listen to the event. The answer is NO in a traditional Publisher/Subscriber design pattern (at least I think it is). I have stated that if nothing is subscribed, then the event never gets
3
4263
by: Mike Cain | last post by:
Hi I have a JS library that I want others in my company to use that uses AttachEvent (etc) to add a method to the onLoad event so that it is called. Using this approach of course is good because then my onLoad event does not clobber that of others programmers in my company that may be already using onLoad for something else (in this case both their onLoad event and mine will fire). However I'm finding that in some cases it is important...
13
1980
by: Dog Ears | last post by:
I've got a windows form that monitors the status of employees it polls a database every 2 seconds to fetch updates then update a underlying dataset bound to a grid using Invoke to marshal to the gui thread. What pattern should I be using now I'm upgrading to .net 2.0? APM, async delegates and BeginInvoke or Thread.Start etc...? Should I be creating a single thread manulally (or using threadpool) and and setting the polling on an...
4
2464
by: Marcolino | last post by:
Hi All, I'm using this code provided by Michael to run Async process: http://groups.google.com/group/microsoft.public.dotnet.languages.vb/browse_thread/thread/3dda80ed45e6176e/606d19fccc330588#606d19fccc330588 Now I have a new need. I have to start a Soubroutine when the Async process is finished. Many Thanks.
0
9722
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
10644
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
10379
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...
1
10393
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9200
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...
1
7664
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5550
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...
0
5690
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4334
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

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.