L
Leslie Sanford
I'm thinking about using the design pattern described at the link below
to solve a problem:
http://msdn2.microsoft.com/en-us/library/ms228963.aspx
I have a question, though, and would appreciate any help.
The pattern describes using BeginOperationName and EndOperationName
methods for asynchronous operations, where "OperationName" represents
the name of the operation to be invoked asynchronously. For example, say
you want to read from a Stream asynchronously. You would call BeginRead,
passing it an AsyncCallback delegate and possibly an object representing
state information. At some point, the stream invokes your callback. In
your callback, you call EndRead, passing it the IAsyncResult object
given to you to get the result of the read operation.
Ok, say we are writing a class to implement this pattern. Instread of
reading bytes, we're reading an object of some type. Let's call the
class ObjectReader. We have the methods BeginReadObject and
EndReadObject. Like the Stream class, this class has a Close method that
disposes of the object.
Where it gets fuzzy for me is trying to figure out what should happen
when the BeginReadObject method is called and the ObjectReader's Close
method is called before the specified callback is invoked.
My thought is that in response to being closed, the ObjectReader should
invoke any awaiting callbacks. When the callback calls EndObjectRead, it
returns null indicating that no more objects are available to be read.
Once all awaiting callbacks have been invoked, the ObjectReader finishes
closing down. At this point, it is officially closed; any attempt to
read from the ObjectReader will throw an ObjectDisposedException.
For BeginReadObject operations that were invoked without a callback,
i.e. null was passed instead of a delegate for the callback parameter,
the wait handles of any remaining IAsyncResult objects are set when the
ObjectReader is closed. Calls to EndReadObject behave just as they would
inside the callback.
Any thoughts?
to solve a problem:
http://msdn2.microsoft.com/en-us/library/ms228963.aspx
I have a question, though, and would appreciate any help.
The pattern describes using BeginOperationName and EndOperationName
methods for asynchronous operations, where "OperationName" represents
the name of the operation to be invoked asynchronously. For example, say
you want to read from a Stream asynchronously. You would call BeginRead,
passing it an AsyncCallback delegate and possibly an object representing
state information. At some point, the stream invokes your callback. In
your callback, you call EndRead, passing it the IAsyncResult object
given to you to get the result of the read operation.
Ok, say we are writing a class to implement this pattern. Instread of
reading bytes, we're reading an object of some type. Let's call the
class ObjectReader. We have the methods BeginReadObject and
EndReadObject. Like the Stream class, this class has a Close method that
disposes of the object.
Where it gets fuzzy for me is trying to figure out what should happen
when the BeginReadObject method is called and the ObjectReader's Close
method is called before the specified callback is invoked.
My thought is that in response to being closed, the ObjectReader should
invoke any awaiting callbacks. When the callback calls EndObjectRead, it
returns null indicating that no more objects are available to be read.
Once all awaiting callbacks have been invoked, the ObjectReader finishes
closing down. At this point, it is officially closed; any attempt to
read from the ObjectReader will throw an ObjectDisposedException.
For BeginReadObject operations that were invoked without a callback,
i.e. null was passed instead of a delegate for the callback parameter,
the wait handles of any remaining IAsyncResult objects are set when the
ObjectReader is closed. Calls to EndReadObject behave just as they would
inside the callback.
Any thoughts?