Hi Bob,
Your analysis is correct. The problem here is that the native type object
"myClass" is passed as a parameter to BeginInvoke. There are two ways to
work around this issue, one is changing your MyClass type from native to
managed, and the other one is using a wrapper managed class to wrap your
native type object. For example:
=====================================
class MyClass {
public:
int Age;
string Name;
};
template <typename T>
ref struct MyWrapperClass
{
MyWrapperClass() : m_ptr(0) {}
MyWrapperClass(T* ptr) : m_ptr(ptr) {}
MyWrapperClass(MyWrapperClass<T>% right) : m_ptr(right.Release())
{
}
T* operator->()
{
return m_ptr;
}
T* Get()
{
return m_ptr;
}
T* Release()
{
T* released = m_ptr;
m_ptr = 0;
return released;
}
private:
T* m_ptr;
};
// Forward references
void BackgroundWorker(MyWrapperClass<MyClassmyWrapperCl s);
void Callback(IAsyncResult ^ar);
// Module-level data
delegate void BackgroundWorkerDelegType(MyWrapperClass<MyClass>
myWrapperCls);
// Main program
int main(array<System::String ^^args)
{
MyClass myClass;
myClass.Age = 26;
myClass.Name="Bill";
MyWrapperClass<MyClassmyWrapperCls(&myClass);
BackgroundWorkerDelegType^ d = gcnew
BackgroundWorkerDelegType(BackgroundWorker);
AsyncCallback^ cb = gcnew AsyncCallback(Callback);
d->BeginInvoke(myWrapperCls, cb, d);
Console::Read();
return 0;
}
void BackgroundWorker(MyWrapperClass<MyClassmyWrapperCl s)
{
Console::WriteLine("In BackgroundWorker");
Console::WriteLine(myWrapperCls->Age);
Console::WriteLine(myWrapperCls->Name.c_str());
}
// Routine called when the asynchronous call completes
void Callback(IAsyncResult ^ar)
{
BackgroundWorkerDelegType^ d =
static_cast<BackgroundWorkerDelegType^>(ar->AsyncState);
d->EndInvoke(ar);
}
================================================
For mix using native and managed types, you may also refer to this article:
Best Practices for Writing Efficient and Reliable Code with C++/CLI
http://msdn.microsoft.com/en-us/libr...37(VS.80).aspx
Hope this helps. If you have any other questions or concerns, please feel
free to let me know.
Best regards,
Charles Wang
Microsoft Online Community Support
================================================== =========
Delighting our customers is our #1 priority. We welcome your
comments and suggestions about how we can improve the
support we provide to you. Please feel free to let my manager
know what you think of the level of service provided. You can
send feedback directly to my manager at:
ms****@microsoft.com.
================================================== =========
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for
non-urgent issues where an initial response from the community
or a Microsoft Support Engineer within 1 business day is acceptable.
Please note that each follow up response may take approximately
2 business days as the support professional working with you may
need further investigation to reach the most efficient resolution.
The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by
contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
================================================== ==========
This posting is provided "AS IS" with no warranties, and confers no rights.
================================================== =======