views:

154

answers:

2

Hi all,

A simple question really, but one where I cannot find any anwsers too.

When I execute an Asynchronous Socket operation, such as :

socket.BeginSend
(
    new byte[]{6},                // byte[]        | buffer
    0,                            // int           | data to send buffer offset
    1,                            // int           | data to send length
    SocketFlags.None,             // enum          | dunno :)
    new AsyncCallback(OnSend),    // AsyncCallback | callback method
    STATEOBJECT                   // object        | ..state object..
);

It works, and when complete it invokes the AsyncCallback parameter, passing with it an IAsyncResult.

void OnSend(IAsyncResult ar)
{
    object STATEOBJECT = ar.AsyncState as object;
    /*
        Process the socket operation
    */
}

SO..

When the socket operation is being executed 'asynchronously' I know from various sources that the buffer is pinned in memory.

However I do not know where the 'state object' is stored?

why? because I am wondering what the effect of large 'state object's will have?

Taa!

+2  A: 

Is C#, the object is stored where is allocated :)

The real question should be: who has a reference to the object during the async operation? The coarse answer is: the framework. A more precise answer is that your object is ultimately referenced by the OVERLAPPED structure, through a mixture of managed and native data types, and the OVERLAPPED structure is kept in a list in the kernel.

Remus Rusanu
Overlapped??? what is this overlapped! thanks more info to digest :)
divinci
If you want to learn about what's *really* happening, get a copy of Inside Windows by Solomo/Russinovich. If you want to use the async IO performantly, go over the articles linked in http://rusanu.com/2008/11/11/high-performance-windows-programs/. The concepts apply to Managet .Net/C# just as well.
Remus Rusanu
Thanks Remus, £4.26 used on amazon :)
divinci
Remus Rusanu
thanks for the information Remus, think I will digest the 2000 3rd edition first :) then compare. Thanks again.
divinci
+2  A: 

It is stored where you created it, on the Heap. And it will not move unless the GarbageCollector finds that necessary. You are just handing out a reference to the BeginSend() method, and you get it back in the OnSend[Complete]() method.

Henk Holterman