Example #1
0
        /// <summary>Begins to serialize the objects to the specified stream using the specified encoding.</summary>
        /// <param name="objects">The collection of objects that must be serialized.</param>
        /// <param name="stream">The stream to which the objects must be serialized.</param>
        /// <param name="encoding">The encoding that must be used to serialize the data.</param>
        /// <param name="callback">The method to be called when the asynchronous operation is completed.</param>
        /// <param name="state">A user-provided object that distinguishes this particular asynchronous request from other requests.</param>
        /// <returns>An <see cref="IAsyncResult"/> that references the asynchronous operation.</returns>
        public IAsyncResult BeginSerialize(IEnumerable <T> objects, Stream stream, Encoding encoding, AsyncCallback callback, object state)
        {
            AsyncResult <int>          result    = new AsyncResult <int>(callback, state);
            SerializePropertyContainer container =
                new SerializeToStreamPropertyContainer {
                Objects = objects, Stream = stream, Encoding = encoding, AsyncResult = result
            };

            ThreadPool.QueueUserWorkItem(this.SerializeToStreamHelper, container);
            return(result);
        }
Example #2
0
        /// <summary>Executes the <see cref="Serialize(IEnumerable{T},Stream,Encoding)"/> method in a separate thread. This is used to support
        /// asynchronous operations.</summary>
        /// <param name="propertyContainer">The object that holds properties that are required for the asynchronous operation.</param>
        private void SerializeToStreamHelper(object propertyContainer)
        {
            Guard.ArgumentIsOfType <SerializeToStreamPropertyContainer>(propertyContainer, nameof(propertyContainer), "The specified object was not of the expected type SerializeToStreamPropertyContainer");
            SerializeToStreamPropertyContainer container = propertyContainer as SerializeToStreamPropertyContainer;

            try {
                int result = this.Serialize(container.Objects, container.Stream, container.Encoding);
                container.AsyncResult.SetAsCompleted(result, null, false);
            }
            catch (Exception ex) {
                /* We deliberately catch every exception. It is up to the caller of EndSerialize() to do a more fine-grained exception handling */
                container.AsyncResult.SetAsCompleted(-1, ex, false);
            }
        }