예제 #1
0
 private void UpdateDownloadStatus(DownloadTask task, ListViewItem item, int current, int total, int completed, string status)
 {
     item.SubItems[0].Text = task.Url;
     item.SubItems[1].Text = total == -1 ? "Unknown" : string.Format("{0:0.00} KB", (float)total / 1024f);
     item.SubItems[2].Text = string.Format("{0:0.00} KB", (float)current / 1024f);
     item.SubItems[3].Text = string.Format("{0:00}%", completed);
     item.SubItems[4].Text = status;
 }
예제 #2
0
        private void InitializeDownloadTasks()
        {
            for (int i = 0; i < _patches.Length; i++)
            {
                ServerPatch patch = _patches[i];

                string url = patch.PatchUrl;
                int version = patch.Version;

                string filename = Path.GetFileName(url);
                string destination = Path.Combine(_serverDirectory, filename);

                DownloadTask task = new DownloadTask(_patches[i].PatchUrl, destination);
                ListViewItem item = new ListViewItem(new string[5]);

                task.ProgressUpdate += new EventHandler<ProgressUpdateEventArgs>(OnDownloadStateProgressUpdate);
                task.ProgressCompleted += new EventHandler<ProgressCompletedEventArgs>(OnDownloadStateProgressCompleted);

                _downloadManager.Queue(task);
                _downloadTable.Add(task, item);

                lvDownloads.Items.Add(item);

                UpdateDownloadStatus(task, item, 0, -1, 0, "Download Queued");
            }
        }
예제 #3
0
        private void InitializeExtractionTask(DownloadTask state)
        {
            string filename = Path.GetFileName(state.Url);
            string archive = state.Destination;

            ListViewItem item = _downloadTable[state];
            ExtractionTask task = new ExtractionTask(state.Destination, Path.GetDirectoryName(state.Destination));

            task.ProgressUpdate += new EventHandler<ProgressUpdateEventArgs>(OnExtractionProgressUpdate);
            task.ProgressCompleted += new EventHandler<ProgressCompletedEventArgs>(OnExtractionProgressCompleted);

            Invoke((MethodInvoker)delegate()
            {
                UpdateExtractionStatus(task, item, 0, 100, 0, "Extraction Queued");
            });

            _extractionTable.Add(task, item);
            _extractionManager.Queue(task);

            if (!_extractionManager.HasBegun)
            {
                _extractionManager.Begin();
            }
        }
예제 #4
0
        protected override void BeginTask(object state)
        {
            DownloadTask task      = (DownloadTask)state;
            Exception    exception = null;

            HttpWebRequest  request  = null;
            HttpWebResponse response = null;

            string directory = Path.GetDirectoryName(task.Destination);

            FileSystemHelper.EnsureDirectoryExists(directory);

            Stream input  = null;
            Stream writer = new FileStream(task.Destination, FileMode.OpenOrCreate);

            long totalBytesToRecieve = 0;
            long bytesRecieved       = 0;

            try
            {
                if (File.Exists(task.Url))
                {
                    input = new FileStream(task.Url, FileMode.Open);
                    totalBytesToRecieve = input.Length;
                }
                else
                {
                    request         = (HttpWebRequest)WebRequest.Create(task.Url);
                    request.Timeout = 60000;
                    response        = (HttpWebResponse)request.GetResponse();

                    if (response.StatusCode != HttpStatusCode.OK)
                    {
                        throw new WebException(String.Format("Error retrieving download.  WebRequest status code = {0}", response.StatusCode));
                    }

                    input = response.GetResponseStream();
                    totalBytesToRecieve = response.ContentLength;
                }

                byte[] buffer = new byte[0x2000];

                bytesRecieved = 0;

                BinaryReader reader   = new BinaryReader(input);
                int          progress = 0;

                if (!CancelRequested)
                {
                    task.OnProgressStarted(this, new ProgressStartedEventArgs(task));
                }

                while (bytesRecieved < totalBytesToRecieve)
                {
                    if (CancelRequested)
                    {
                        break;
                    }

                    int count = (int)Math.Min((long)buffer.Length, totalBytesToRecieve - bytesRecieved);

                    buffer = reader.ReadBytes(count);
                    writer.Write(buffer, 0, count);
                    bytesRecieved += count;

                    int currentProgress = (int)((100 * bytesRecieved) / totalBytesToRecieve);

                    if (currentProgress != progress)
                    {
                        progress = currentProgress;

                        if (!CancelRequested)
                        {
                            task.OnProgressUpdate(this, new ProgressUpdateEventArgs(task, (int)bytesRecieved, (int)totalBytesToRecieve));
                        }
                    }
                }
            }
            catch (Exception e)
            {
                exception = e;
            }
            finally
            {
                if (input != null)
                {
                    input.Close();
                }

                if (writer != null)
                {
                    writer.Close();
                }

                if (!CancelRequested)
                {
                    task.OnProgressCompleted(this, new ProgressCompletedEventArgs(exception, CancelRequested, task, (int)bytesRecieved, (int)totalBytesToRecieve));
                }
            }

            base.BeginTask(state);
        }