Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8813075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:45:13+00:00 2026-06-14T03:45:13+00:00

I am using UdpClient (c++/cli), I start my listener using the obvious BeginReceive. System::Void

  • 0

I am using UdpClient (c++/cli), I start my listener using the obvious BeginReceive.

System::Void CUdpTransmitter::StartListener() {
  ...
  m_UdpClient->BeginReceive ( gcnew System::AsyncCallback(ReceiveCallback), this );
}

ReceiveCallback should start new AsyncCallback at the end. Is there any performance benefit or any other reason to store the AsyncCallback in a member variable, rather than allocate a new one on each call? What about thread safety? Compare following variants:

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 );
    }
  }
}

Thanks for your time and your answers.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T03:45:14+00:00Added an answer on June 14, 2026 at 3:45 am

    Delegates are thread safe, so there’s no worry about that. (Delegates are immutable after creation, any method that adds or removes a delegate from a multicast delegate is actually returning a new multicast delegate.)

    There’s a very minor performance penalty for instantiating the delegate objects and garbage collecting them, so if you’re using this delegate very frequently, then go with m_callback. However, as I said, this is a very minor penalty unless you’re doing it several million times per second.

    If you’re not doing this frequently, and you want to clean up your code and have less class members hanging around, go ahead and instantiate the callback each time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I create a socket using var socket = new UdpClient(0,AddressFamily.InterNetwork); How do I
I am maintaining other's code and its using the class UdpClient . The code
I am using a UdpClient to send packets to a server. I am initializing
I'm using a UdpClient at the server end and it is sending data to
I'm not exactly sure if the following scenario is possible. Using only UdpClient or
I'm using UDPClient like below dim c = New UDPClient(port) client.CLient.ReceiveTimeout = 1 await
Using online interfaces to a version control system is a nice way to have
I'm building a device discovery system using a UDP broadcast message. I started out
I am using the UDPClient class to send and receive messages on my loopback
http://msdn.microsoft.com/en-us/library/ekd1t784.aspx The JoinMulticastGroup method subscribes the UdpClient to a multicast group using the specified

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.