none
MSDN SocketAsyncEventArgs example RRS feed

  • Question

  • Hi All,

    So I'm kind of new to programming and I have been going over the SocketAsyncEventArgs sample on MSDN, and I have a question about the threading. Heres a link to the example msdn.microsoft.com/en-us/library/system.net.sockets.socketasynceventargs.aspx. So I'll get right to the question and apologize in advance for the garbled spew I spit out.

    1) In the StartAccept() method I'm assuming that I enter the method in my main app thread.

    2) if ((!willraiseevent) == true) would complete synchronously in the same thread.

    3) if ((!willraiseevent) != true) would spew a new thread waiting until a new connection is accepted and then throw the acceptEventArg.Completed on the original thread because it is Asynchronous?

    Thanks for any help!

    Wednesday, December 23, 2015 4:26 AM

Answers

  • 1) In the StartAccept() method I'm assuming that I enter the method in my main app thread.

    Yes, or whatever thread the method was called on.

    2) if ((!willraiseevent) == true) would complete synchronously in the same thread.

    Yes, the Socket.AcceptAsync method will begin an asynchronous operation. Once it has eventually completed, the Completed event on the passed in acceptEventArgs will be raised on a thread pool thread.

    If the method returns true, there already is a pending operation and the ProcessAccept method won't be called.


    3) if ((!willraiseevent) != true) would spew a new thread waiting until a new connection is accepted and then throw the acceptEventArg.Completed on the original thread because it is Asynchronous?

    The ProcessAccept method is called on the same thread as the StartAccept method. It calls the ReceiveAsync method that will begin an asynchronous request to receive data from the socket. You are still on the same thread.

    The callbacks, IO_Completed and AcceptEventArg_Completed, will be invoked on a thread pool thread though. But the sample code doesn't create any additional background threads.

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    Wednesday, December 23, 2015 11:11 AM
  • Please start a new thread when you have a new question.

    >>I always assumed async calls returned to the calling thread on completion, so I assumed the SocketAsyncEventArgs.Completed event would be sent to the calling thread ... This isn't true?

    It depends on the SynchronizationContext. The current SynchronizationContext is captured just before the method is being awaited. In a UI application, the continuation will be run on the same thread but in a console application it will be run on a ThreadPool thread. Please refer to the following MSDN blog post for more information about this: http://blogs.msdn.com/b/pfxteam/archive/2012/01/20/10259049.aspx

    Please remember to close your threads by marking all helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Marked as answer by MysticKnight1 Tuesday, January 5, 2016 1:22 PM
    Sunday, January 3, 2016 10:59 AM

All replies

  • 1) In the StartAccept() method I'm assuming that I enter the method in my main app thread.

    Yes, or whatever thread the method was called on.

    2) if ((!willraiseevent) == true) would complete synchronously in the same thread.

    Yes, the Socket.AcceptAsync method will begin an asynchronous operation. Once it has eventually completed, the Completed event on the passed in acceptEventArgs will be raised on a thread pool thread.

    If the method returns true, there already is a pending operation and the ProcessAccept method won't be called.


    3) if ((!willraiseevent) != true) would spew a new thread waiting until a new connection is accepted and then throw the acceptEventArg.Completed on the original thread because it is Asynchronous?

    The ProcessAccept method is called on the same thread as the StartAccept method. It calls the ReceiveAsync method that will begin an asynchronous request to receive data from the socket. You are still on the same thread.

    The callbacks, IO_Completed and AcceptEventArg_Completed, will be invoked on a thread pool thread though. But the sample code doesn't create any additional background threads.

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    Wednesday, December 23, 2015 11:11 AM
  • I always assumed async calls returned to the calling thread on completion, so I assumed the SocketAsyncEventArgs.Completed event would be sent to the calling thread ... This isn't true?

    Main_Thread        New_Thread

    CreateSocket

    Listen

    AcceptAsync -----> AcceptSocket

    OtherServerStuff            |

                                 AcceptCompletes

    AcceptCallBack

    ProcessAccept

    Saturday, January 2, 2016 12:35 AM
  • Please start a new thread when you have a new question.

    >>I always assumed async calls returned to the calling thread on completion, so I assumed the SocketAsyncEventArgs.Completed event would be sent to the calling thread ... This isn't true?

    It depends on the SynchronizationContext. The current SynchronizationContext is captured just before the method is being awaited. In a UI application, the continuation will be run on the same thread but in a console application it will be run on a ThreadPool thread. Please refer to the following MSDN blog post for more information about this: http://blogs.msdn.com/b/pfxteam/archive/2012/01/20/10259049.aspx

    Please remember to close your threads by marking all helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Marked as answer by MysticKnight1 Tuesday, January 5, 2016 1:22 PM
    Sunday, January 3, 2016 10:59 AM