Example #1
0
        /// <summary>Begins to deserialize the objects from the specified file using the specified encoding.</summary>
        /// <param name="filePath">The name of the input file.</param>
        /// <param name="encoding">The encoding that must be used to deserialize 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(string filePath, Encoding encoding, AsyncCallback callback, object state)
        {
            AsyncResult <ICollection <T> > result    = new AsyncResult <ICollection <T> >(callback, state);
            DeserializePropertyContainer   container =
                new DeserializeFromFilePropertyContainer {
                FilePath = filePath, Encoding = encoding, AsyncResult = result
            };

            ThreadPool.QueueUserWorkItem(this.DeserializeToFileHelper, container);
            return(result);
        }
Example #2
0
        /// <summary>Executes the <see cref="Deserialize(string,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 DeserializeToFileHelper(object propertyContainer)
        {
            Guard.ArgumentIsOfType <DeserializeFromFilePropertyContainer>(propertyContainer, nameof(propertyContainer), "The specified object was not of the expected type DeserializeFromFilePropertyContainer");
            DeserializeFromFilePropertyContainer container = propertyContainer as DeserializeFromFilePropertyContainer;

            try {
                ICollection <T> result = this.Deserialize(container.FilePath, 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);
            }
        }