473,385 Members | 1,355 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,385 software developers and data experts.

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.BeginInvoke(message, null, this);
}

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

SyncEvent(message);
}

}
II. Event Testing in MC++, which doesn't compile correctly.
public delegate bool MCDelegate(System::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(errorCode));
delg.BeginInvoke(NULL, delg);
}

// Works fine
void FireEventSync(System::String^ errorCode)
{
EventSync(errorCode);
}
};

Thanks for any suggestions!
Nov 6 '06 #1
9 5714
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(errorCode));
MyErrorDelegate^ delg = gcnew MyErrorDelegate(this,
&MCEventTesting::EventASync);

The first argument (this) is a handle to the handler class. The second
argument (&MCEventTesting::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(errorCode));

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

The first argument (this) is a handle to the handler class. The second
argument (&MCEventTesting::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(System::String^ errorCode);

So the event handler must be declared

bool xxx(System::String^ 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(System::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 fireErrorEventAsync(System::String^ errorCode)
{
// ERROR: "Complains that it must be a pointer to a member function."
MCDelegate^ delg = gcnew MCDelegate(this,&MCEventTesting::AsyncEvent);
AsyncEvent->BeginInvoke("test",nullptr,this);

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

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

"BartMan" <Ma******@community.nospamwrote in message
news:9F**********************************@microsof t.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(System::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 fireErrorEventAsync(System::String^ errorCode)
{
// ERROR: "Complains that it must be a pointer to a member function."
MCDelegate^ delg = gcnew MCDelegate(this,&MCEventTesting::AsyncEvent);
AsyncEvent->BeginInvoke("test",nullptr,this);

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

private:
MCDelegate^ AsyncHandlerList;

public:
event MCDelegate^ Async {
MCDelegate^ add(MCDelegate^ d) { return AsyncHandlerList =
Delegate::Combine(AsyncHandlerList, d); }
MCDelegate^ remove(MCDelegate^ d) { return AsyncHandlerList =
Delegate::Remove(AsyncHandlerList, d); }
}

void fireErrorEventAsync(System::String^ errorCode)
MCEventTesting::AsyncHandlerList->BeginInvoke(errorCode,nullptr,nullptr);
}
>
// Works fine.
void fireSyncEvent(System::String^ errorCode)
{
SyncEvent(errorCode);
}
};

Nov 7 '06 #6
event MCDelegate^ Async {
MCDelegate^ add(MCDelegate^ d) { return AsyncHandlerList =
Delegate::Combine(AsyncHandlerList, d); }
MCDelegate^ remove(MCDelegate^ d) { return AsyncHandlerList =
Delegate::Remove(AsyncHandlerList, 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******@community.nospamwrote in message
news:A7**********************************@microsof t.com...
>event MCDelegate^ Async {
MCDelegate^ add(MCDelegate^ d) { return AsyncHandlerList =
Delegate::Combine(AsyncHandlerList, d); }
MCDelegate^ remove(MCDelegate^ d) { return AsyncHandlerList =
Delegate::Remove(AsyncHandlerList, 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::EventHandler^ Closed

{

void add( System::EventHandler^ handler )

{

CloseHandlers += handler;

}

void remove( System::EventHandler^ 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::EventHandler^ Closed

{

void add( System::EventHandler^ handler )

{

CloseHandlers += handler;

}

void remove( System::EventHandler^ 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(System::String^ errorCode);

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

event MCDelegate^ SyncEvent;

private:
MCDelegate^ AsyncHandlerList;

public:
event MCDelegate^ Async
{

void add(MCDelegate^ d)
{
AsyncHandlerList =
(MCDelegate^)System::Delegate::Combine(AsyncHandle rList, d);
}

void remove(MCDelegate^ d)
{
AsyncHandlerList = (MCDelegate^)System::Delegate::Remove(AsyncHandler List,
d);
}
}

public:
void fireErrorEventAsync(System::String^ errorCode)
{
MCEventTesting::AsyncHandlerList->BeginInvoke(errorCode,nullptr,nullptr);
}

void fireSyncEvent(System::String^ errorCode)
{
SyncEvent(errorCode);
}
};
Nov 7 '06 #9
"BartMan" <Ma******@community.nospamwrote in message
news:34**********************************@microsof t.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(System::String^ errorCode);

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

event MCDelegate^ SyncEvent;

private:
MCDelegate^ AsyncHandlerList;

public:
event MCDelegate^ Async
{

void add(MCDelegate^ d)
{
AsyncHandlerList =
(MCDelegate^)System::Delegate::Combine(AsyncHandle rList, d);
As I showed you with my code, this can be shortened to: AsyncHandlerList +=
d
}

void remove(MCDelegate^ d)
{
AsyncHandlerList = (MCDelegate^)System::Delegate::Remove(AsyncHandler List,
d);
And here, AsyncHandlerList -= d
}
}

public:
void fireErrorEventAsync(System::String^ errorCode)
{
MCEventTesting::AsyncHandlerList->BeginInvoke(errorCode,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(System::String^ errorCode)
{
SyncEvent(errorCode);
}
};

Nov 7 '06 #10

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

Similar topics

6
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...
8
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),...
10
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...
2
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...
5
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...
4
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...
3
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...
13
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...
4
by: Marcolino | last post by:
Hi All, I'm using this code provided by Michael to run Async process: ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

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.