Tech Off Thread

8 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Async-Operations Question

Back to Forum: Tech Off
  • User profile image
    Shark_M

    Hi guys,
    I am not sure about something. When you do async-operations, in .net 2.0, what actually happens? When you do BeginSomething (like BeginAccept or BeginRecieve in Sockets), what is actually happening?

    My understanding is that, When you do BeginAccept, a workitem is sent to the threadpool of the system, and then a request is made at the device driver to create nessessary resources to begin the acceptance process. Then at what point will you callback method get fired? and under what thread? Is it the executing thread or a seperate thread from the system ThreadPool?

    Also, when you say Endrecieve or Endsomething, after you get the IAsyncresult, what does that do in the background? Does it end the thread task from the Thread pool and then this would mean you have to request BeginSomething (like beginRecieve) again in a loop?

    so like this?

    Socket.BeginAccept(something,something...);
                                   .....
    current thread submit a new work item to the threadPool of the system, and does not wait.
                                   .....
    (the system makes available resources to accept connection)
                                   .....
    The system Fires the Callback method (Using Thread from thread pool? or just signals the main executing thread in ur app, to fire async callback method?)
                                   .....
    Callbackmethod fired, and you end the AsyncOperation, (what does this mean in the background?, does threads get killed? or just work items removed or ????)
                                    .....
    start the whole process again by calling BeginSomething (like BeginAccept or BeginReceive or ...)

    So the important thing here, is what happens when you call BeginAccept? and which thread executes the CallBackMethod?
    how multithreading play part here if you want to use seperate threads to improve performance?

    Thanks again

  • User profile image
    pacelvi

    Shark_M wrote:
    Hi guys,
    I am not sure about something. When you do async-operations, in .net 2.0, what actually happens? When you do BeginSomething (like BeginAccept or BeginRecieve in Sockets), what is actually happening?

    My understanding is that, When you do BeginAccept, a workitem is sent to the threadpool of the system, and then a request is made at the device driver to create nessessary resources to begin the acceptance process. Then at what point will you callback method get fired? and under what thread? Is it the executing thread or a seperate thread from the system ThreadPool?


    What happens is a new thread from the ThreadPool is provided the delegate to the method you want to run. The method will then execute on that thread.  When the delegate is BeginInvoked, it returns a IAsynchResult so that you can refer back to the instance being run.

    When your originating thread continues on, you can use the IAR to provide a wait handle or  if you provided a Callback to BeginInvoke, the system will automatcally call that Callback at the completion of the method that was running in the ThreadPool.  This is usually on a 3rd thread.

    Shark_M wrote:


    Also, when you say Endrecieve or Endsomething, after you get the IAsyncresult, what does that do in the background? Does it end the thread task from the Thread pool and then this would mean you have to request BeginSomething (like beginRecieve) again in a loop?


    EndX provides you with the method's return value or if an exception occured in your method, at this point the exception is raised.


    Shark_M wrote:

    so like this?

    Socket.BeginAccept(something,something...);
                                   .....
    current thread submit a new work item to the threadPool of the system, and does not wait.
                                   .....
    (the system makes available resources to accept connection)
                                   .....
    The system Fires the Callback method (Using Thread from thread pool? or just signals the main executing thread in ur app, to fire async callback method?)


    It will Callback on a "3rd" thread.  In other words.. not the thread that started the delegate and not the thread the delegate ran on.


    Shark_M wrote:


                                   .....
    Callbackmethod fired, and you end the AsyncOperation, (what does this mean in the background?, does threads get killed? or just work items removed or ????)



    The thread returns to the ThreadPool

    Shark_M wrote:


                                    .....
    start the whole process again by calling BeginSomething (like BeginAccept or BeginReceive or ...)

    So the important thing here, is what happens when you call BeginAccept? and which thread executes the CallBackMethod?
    how multithreading play part here if you want to use seperate threads to improve performance?


    As long as you dont go crazy BeginInvoking in a loop, you should be ok.  The ThreadPool is a limited resource and if you're looping uncontrollably you can exhaust it very easily.

    Shark_M wrote:



    Thanks again

  • User profile image
    Shark_M

    can you draw an outline for me to show the whole process diagramatically if possible? ( i am interested in what thread runs what, and in what thread call back is run).


    Suppose you have this

    Accept Clients Asynchronusly


    Socket Hanlder = ar.endaccept


    while (ContinueLitening)
                    {
                        // Set the event to nonsignaled state.
                        allDone.Reset();
                        // Start an asynchronous socket to listen for connections.
                        this.listener.BeginAccept(new AsyncCallback(AcceptCallback),this.listener);
                        // Wait until a connection is made before continuing.
                        allDone.WaitOne();
                    }

    in the callback

    Socket handler = ((Socket)ar.AsyncState).EndAccept(ar);

    ---------------------------------------------------------------

    so i have done in a loop started an async operation to accept new connections., the main thread will not resume the loop until it is signaled to continue processing, by the AsyncCallBack method.
    So now we have 2 theads, one is paused, and the other is threadpool thead that is processing the accepted connection.

    When the AsyncCallBackMethod, is fired, it is fired on what thread? The Thread that called listener.BeginAcccept (that is paused) or in the threadpool thread?

    From what i can gather its on the threadpool thread, and my Main Thread continue the loop for the next one.



  • User profile image
    pacelvi

    Generic Forum Image

  • User profile image
    Shark_M

    So the Main Thread never executes the CallBackMethod.
    Infact another thread from the ThreadPool executes it.

    And so does that mean that my Main Thread will never execute the call back ever during this process? When does the contex switch back to my main thread?

    I see in your diagram that after EndCallBack is recived in the ThreadPool #2, The both of the threadpool threads return to the threadpool, and perhapse at that time the context switch back to the main thread is that true?

    What happens if I kill the thread that initiated the AcceptEndBack? suppose I opened this call, using another background thread, and this thread went to handle another process (doing some other work), does this affect what is happening with the 2 ThreadPool threads? Does the main thread need to get any signal later on? or no?


    When Does this process End? when do i have to BeginX again? Does it end once we Call EndX, and returns the value to the IAR? (Meaning when you BeginRecieve on a socket, that means its only 1 run, and you have to repeat again, in a loop the whole process)?

    Thanks alot

  • User profile image
    pacelvi

    Shark_M wrote:
    So the Main Thread never executes the CallBackMethod.
    Infact another thread from the ThreadPool executes it.


    Correct

    Shark_M wrote:


    And so does that mean that my Main Thread will never execute the call back ever during this process? When does the contex switch back to my main thread?


    It follows that if the Callback is executed on another thread from the threadpool that the main thread doesn't execute it.

    The context switch occurs whenever the OS schedules the CPU to run the thread.  Threads execute independently of one another (that's why they exist)


    Shark_M wrote:



    I see in your diagram that after EndCallBack is recived in the ThreadPool #2, The both of the threadpool threads return to the threadpool, and perhapse at that time the context switch back to the main thread is that true?


    Presumedly by the time EndInvoke is called your main thread would have been already running for a while.


    Shark_M wrote:



    What happens if I kill the thread that initiated the AcceptEndBack? suppose I opened this call, using another background thread, and this thread went to handle another process (doing some other work), does this affect what is happening with the 2 ThreadPool threads? Does the main thread need to get any signal later on? or no?



    The Asynchronius Delegate pattern was not designed for this type of interaction.  The pattern is  to run short asych methods which expire fairly quickly.  That's why they use the limited thread pool resource.


    Shark_M wrote:




    When Does this process End?


    Your process ends when your main thread ends.

    Shark_M wrote:



    when do i have to BeginX again?


    That's your design decision

    Shark_M wrote:


    Does it end once we Call EndX, and returns the value to the IAR? (Meaning when you BeginRecieve on a socket, that means its only 1 run, and you have to repeat again, in a loop the whole process)?

    You'll have to read the docs about reading the socket. I dont know enough about it to comment.

    Shark_M wrote:



    Thanks alot

  • User profile image
    Shark_M

    Thanks alot, your help is greatly appreciated.

    The reason i asked these question is because i am studying the mechanism, (ie what happens behind the scenes). So now a few things needs to be clarified here and their.

    so thanks alot for you input into this matter. It helped me alot

    If you dont mind, could you recommend a good book that would explain things like that in more detail? perferably in C# or VB.NET (2005)

    Regards,

  • User profile image
    Shark_M

    I just got one question regarding BeginX EndX.

    When you call BeginRecieve on a socket, When the callback method is called, what happens to the data that was read in the stream? does it get flushed automatically? or does it stay in the buffer, untill another data comes in?

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.