UdpClient (c++/cli) を使用しています。明らかな BeginReceive を使用してリスナーを開始します。
System::Void CUdpTransmitter::StartListener() {
...
m_UdpClient->BeginReceive ( gcnew System::AsyncCallback(ReceiveCallback), this );
}
ReceiveCallback は、最後に新しい AsyncCallback を開始する必要があります。呼び出しごとに新しいものを割り当てるのではなく、メンバー変数に AsyncCallback を格納するパフォーマンス上の利点やその他の理由はありますか? スレッドセーフはどうですか?次のバリアントを比較します。
System::Void CUdpTransmitter::ReceiveCallback1( System::IAsyncResult ^ asyncResult ) {
m_UdpClient->EndReceive();
// parse received data (not time consumpting)
...
if (! exit) {
m_UdpClient->BeginReceive ( gcnew System::AsyncCallback(ReceiveCallback), self );
}
}
public ref class CUdpTransmitter {
AsyncCallback ^ m_callback; // store AsyncCallback in a member variable, it will be initized in constructor... gcnew System::AsyncCallback(ReceiveCallback2)
System::Void CUdpTransmitter::ReceiveCallback2( System::IAsyncResult ^ asyncResult ) {
m_UdpClient->EndReceive();
// parse received data (not time consumpting)
...
if (! exit) {
// start receiving, reuse the member variable and avoid gcnew
m_UdpClient->BeginReceive ( m_callback, self );
}
}
}
お時間とご回答ありがとうございます。