/// <summary>Begins to deserialize the objects from the specified stream using the specified encoding.</summary> /// <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 BeginDeserialize(Stream stream, Encoding encoding, AsyncCallback callback, object state) { AsyncResult <ICollection <T> > result = new AsyncResult <ICollection <T> >(callback, state); DeserializePropertyContainer container = new DeserializeFromStreamPropertyContainer { Stream = stream, Encoding = encoding, AsyncResult = result }; ThreadPool.QueueUserWorkItem(this.DeserializeToStreamHelper, container); return(result); }
/// <summary>Executes the <see cref="Deserialize(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 DeserializeToStreamHelper(object propertyContainer) { Guard.ArgumentIsOfType <DeserializeFromStreamPropertyContainer>(propertyContainer, nameof(propertyContainer), "The specified object was not of the expected type DeserializeFromStreamPropertyContainer"); DeserializeFromStreamPropertyContainer container = propertyContainer as DeserializeFromStreamPropertyContainer; try { ICollection <T> result = this.Deserialize(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(null, ex, false); } }