How to call Invoke in C++/CLI?????????????????????????

  • Thread starter Thread starter Sam Carleton
  • Start date Start date
S

Sam Carleton

I have a brain dead simple question: how in the heck do I call invoke
in C++/CLI? I am simply doing a check to see if invoke needs to be
called, but then I cannot for the world figure out how to actual call
Invoke. Nor am I finding an examples anywhere. I know they are out
there, but I simply am not finding them. Please help!!!!!!

In C#:
public delegate void OnStatusHandler(Object sender, StatusEventArgs
args);

In C++/CLI (.Net 2.0)

Void ErrorHdlrDlg::OnInstrStatus_Changed(Object^ sender,
StatusEventArgs^ e)
{
if( this->InvokeRequired)
{
// somehow call invoke on OnInstrStatus_Changed
}
else
{
}
}
 
Void ErrorHdlrDlg::OnInstrStatus_Changed(Object^ sender,
StatusEventArgs^ e)
{
if( this->InvokeRequired)
{
OnInstrStatus_ChangedHandler^ _Handler =
gcnew OnInstrStatus_ChangedHandler(this,
&ErrorHdlrDlg::OnInstrStatus_Changed);
_Handler->Invoke();
return;
}
else
{
}
}
 
Ziad said:
Void ErrorHdlrDlg::OnInstrStatus_Changed(Object^ sender,
StatusEventArgs^ e)
{
if( this->InvokeRequired)
{
OnInstrStatus_ChangedHandler^ _Handler =
gcnew OnInstrStatus_ChangedHandler(this,
&ErrorHdlrDlg::OnInstrStatus_Changed);
_Handler->Invoke();
return;
}
else
{
}
}

That won't do what the OP wanted.

1. The sender and event args parameters are lost.
2. The function will be invoked in the original thread, not in the GUI
thread as was intended.

Instead, you need to use somthing like this:

<code>
void ErrorHdlrDlg::OnInstrStatus_Changed(
Object^ sender,
StatusEventArgs^ e)
{
if (this->InvokeRequired)
{
// use Control::Invoke to marshall the call to the GUI thread
cli::array<Object^>^ oa = gcnew cli::array<Object^>(2);
oa[0] = sender;
oa[1] = e;
this->Invoke(gcnew
OnStatusHandler(this,&ErrorHdlrDlg::OnInstrStatus_Changed), oa);
}
else
{
// normal event handling
}
}
</code>
 
Back
Top