// 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 (AsyncOperationProgress != null)
            {
                AsyncOperationProgress(this, e);
            }
        }
        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);
        }
        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.º 4
0
        /// <summary>takes our copy of the stream, and puts it into the request stream</summary>
        protected void CopyRequestData()
        {
            if (_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();
                Request.ContentLength = _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)_requestCopy.Length / size));
                    }

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

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

                    _requestCopy.Seek(0, SeekOrigin.Begin);

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