Exemplo n.º 1
0
        // This method is invoked via the AsyncOperation object,
        // so it is guaranteed to be executed on the correct thread.
        private void OnAsyncReportProgress(object state)
        {
            AsyncOperationProgressEventArgs e = state as AsyncOperationProgressEventArgs;

            if (this.AsyncOperationProgress != null)
            {
                this.AsyncOperationProgress(this, e);
            }
        }
Exemplo n.º 2
0
        internal bool SendProgressData(AsyncData data, AsyncOperationProgressEventArgs args)
        {
            // In this case, don't allow cancellation, as the method
            // is about to raise the completed event.
            bool ret = !CheckIfOperationIsCancelled(data.UserData);

            if (ret)
            {
                data.Operation.Post(data.Delegate, args);
            }
            return(ret);
        }
Exemplo n.º 3
0
        private MemoryStream CopyResponseToMemory(AsyncData data, Stream responseStream, long contentLength)
        {
            if (responseStream == null)
            {
                return(null);
            }

            // read the stream into memory. That's the only way to satisfy the "main work
            // on the other thread requirement
            MemoryStream memStream = new MemoryStream();
            const int    size      = 4096;
            var          bytes     = new byte[size];

            int    numBytes;
            double current      = 0;
            long   bytesWritten = 0;

            while ((numBytes = responseStream.Read(bytes, 0, size)) > 0)
            {
                memStream.Write(bytes, 0, numBytes);
                if (data == null || data.Delegate == null)
                {
                    continue;
                }
                bytesWritten += numBytes;

                if (contentLength > size)
                {
                    current = bytesWritten * 100d / contentLength;
                }

                // see if we are still in the list...
                // Multiple threads will access the task dictionary,
                // so it must be locked to serialize access.
                if (CheckIfOperationIsCancelled(data.UserData))
                {
                    throw new ArgumentException("Operation was cancelled");
                }

                var args = new AsyncOperationProgressEventArgs(contentLength,
                                                               bytesWritten,
                                                               (int)current,
                                                               data.UriToUse,
                                                               data.HttpVerb,
                                                               data.UserData);
                data.Operation.Post(data.Delegate, args);
            }

            memStream.Seek(0, SeekOrigin.Begin);

            return(memStream);
        }
Exemplo n.º 4
0
        /// <summary>takes our copy of the stream, and puts it into the request stream</summary>
        protected void CopyRequestData()
        {
            if (this.requestCopy != null)
            {
                // Since we don't use write buffering on the WebRequest object,
                // we need to ensure the Content-Length field is correctly set
                // to the length we want to set.
                EnsureWebRequest();
                this.Request.ContentLength = this.requestCopy.Length;
                // stream it into the real request stream
                Stream req = base.GetRequestStream();

                try
                {
                    const int size  = 4096;
                    byte[]    bytes = new byte[size];
                    int       numBytes;

                    double oneLoop = 100;
                    if (requestCopy.Length > size)
                    {
                        oneLoop = (100 / ((double)this.requestCopy.Length / size));
                    }

                    // 3 lines of debug code
                    // this.requestCopy.Seek(0, SeekOrigin.Begin);

                    // StreamReader reader = new StreamReader( this.requestCopy );
                    // string text = reader.ReadToEnd();

                    this.requestCopy.Seek(0, SeekOrigin.Begin);

                    long   bytesWritten = 0;
                    double current      = 0;
                    while ((numBytes = this.requestCopy.Read(bytes, 0, size)) > 0)
                    {
                        req.Write(bytes, 0, numBytes);
                        bytesWritten += numBytes;
                        if (this.asyncData != null && this.asyncData.Delegate != null &&
                            this.asyncData.DataHandler != null)
                        {
                            AsyncOperationProgressEventArgs args;
                            args = new AsyncOperationProgressEventArgs(this.requestCopy.Length,
                                                                       bytesWritten, (int)current,
                                                                       this.Request.RequestUri,
                                                                       this.Request.Method,
                                                                       this.asyncData.UserData);
                            current += oneLoop;
                            if (!this.asyncData.DataHandler.SendProgressData(asyncData, args))
                            {
                                break;
                            }
                        }
                    }
                }
                finally
                {
                    req.Close();
                }
            }
            else
            {
                if (this.IsBatch)
                {
                    this.EnsureWebRequest();
                    this.ContentStore.SaveToXml(this.GetRequestStream());
                    this.CopyRequestData();
                }
            }
        }