当前位置: 首页>>代码示例>>C#>>正文


C# BackgroundWorker.Report方法代码示例

本文整理汇总了C#中System.ComponentModel.BackgroundWorker.Report方法的典型用法代码示例。如果您正苦于以下问题:C# BackgroundWorker.Report方法的具体用法?C# BackgroundWorker.Report怎么用?C# BackgroundWorker.Report使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.ComponentModel.BackgroundWorker的用法示例。


在下文中一共展示了BackgroundWorker.Report方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Download

        public static bool Download(string destinationPath, Uri sourceURL, BackgroundWorker bw)
        {
            int bufferSize = 1024 * 1024;
            long existLen = 0;

            var response = new ComplexResponse();

            System.IO.FileStream saveFileStream = null;

            try
            {
                System.IO.Stream resStream;
                System.Net.HttpWebRequest httpReq;
                System.Net.HttpWebResponse httpRes;
                httpReq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(sourceURL);
                httpReq.Proxy = null;

                response.FileSize = GetFileSize(sourceURL);

                if (System.IO.File.Exists(destinationPath) ) //check only when it accepts ranges at all
                {
                    System.IO.FileInfo destinationFileInfo = new System.IO.FileInfo(destinationPath);
                    existLen = destinationFileInfo.Length;
                    response.Message = string.Format("File exists and has {0} bytes.", existLen);
                    bw.Report(response);
                }

                if (existLen >= response.FileSize)
                    return true;

                httpReq.AddRange(existLen);
                httpRes = (System.Net.HttpWebResponse)httpReq.GetResponse();

                var acceptRanges = String.Compare(httpRes.Headers["Accept-Ranges"], "bytes", true) == 0; //check if server accepts ranges

                if (existLen > 0 && acceptRanges) //if retry is available
                    saveFileStream = new System.IO.FileStream(destinationPath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
                else
                    saveFileStream = new System.IO.FileStream(destinationPath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);

                resStream = httpRes.GetResponseStream();
                response.TotalBytes = httpRes.ContentLength;
                response.Message = string.Format("Remote file has {0} bytes left.", response.TotalBytes);
                bw.Report(response);

                if (response.TotalBytes < 0)
                    throw new WebException("File has no data or does not exist");

                int byteSize;

                byte[] downBuffer = new byte[bufferSize];
                while ((byteSize = resStream.Read(downBuffer, 0, downBuffer.Length)) > 0)
                {
                    if (bw.CancellationPending == true)
                    {
                        response.Message = "Cancelling";
                        bw.Report(response);
                        httpRes.Close();
                        break;
                    }
                    response.CurrentBytes += byteSize;
                    saveFileStream.Write(downBuffer, 0, byteSize);
                    {
                        response.Message = string.Empty;
                        response.Percent = (response.CurrentBytes * 100.0 / response.TotalBytes);
                        if (response.Percent > 100)
                            response.Percent = 100;
                        bw.Report(response);

                    }
                }
                saveFileStream.Close();
                response.Message = string.Format("Downloaded {0} bytes.", response.CurrentBytes);
                bw.Report(response);
                return response.CurrentBytes == response.TotalBytes;
            }
            catch (Exception ex)
            {
                response.Message = string.Format("{0} - {1}", ex.GetType(), ex.Message);
                response.IsError = true;
                bw.Report(response);
                return false;
            }
            finally
            {
                if (saveFileStream != null)
                    saveFileStream.Dispose();
            }
        }
开发者ID:barwis,项目名称:WoT_modDownloader,代码行数:89,代码来源:DownloadHelper.cs


注:本文中的System.ComponentModel.BackgroundWorker.Report方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。