예제 #1
0
        public void btnRemoveAll_Click(System.Object sender, System.EventArgs e)
        {
            DownloadFileAsyncExtended wClient = default(DownloadFileAsyncExtended);

            //// Always loop backwards when removing items from the list,
            //// because the index gets updated when an item is removed.
            //// This can result in certain items not getting removed.
            for (int i = ListViewEx.Items.Count - 1; i >= 0; i--)
            {
                if (ListViewEx.Items[i].Tag != null)
                {
                    //// Get the DownloadFileAsyncExtended class instance from the ListViewItem Tag.
                    wClient = (DownloadFileAsyncExtended)(ListViewEx.Items[i].Tag);
                    //// Pause (cancel) the download and remove it from the list.
                    wClient.CancelAsync();
                    ListViewEx.Items[i].Tag = null;
                    ListViewEx.Items[i].Remove();
                }
                else
                {
                    //// There's nothing to cancel, because the
                    //// download has finished or caused an error.
                    //// Just remove the item from the list.
                    ListViewEx.Items[i].Remove();
                }
            }
        }
예제 #2
0
        public void btnPauseAll_Click(System.Object sender, System.EventArgs e)
        {
            DownloadFileAsyncExtended wClient = default(DownloadFileAsyncExtended);

            for (int i = 0; i <= ListViewEx.Items.Count - 1; i++)
            {
                if (ListViewEx.Items[i].Tag != null)
                {
                    wClient = (DownloadFileAsyncExtended)(ListViewEx.Items[i].Tag);
                    //// Pause the download.
                    wClient.CancelAsync();
                }
            }
        }
예제 #3
0
        public void FormMain_FormClosing(object sender, System.Windows.Forms.FormClosingEventArgs e)
        {
            DownloadFileAsyncExtended wClient = default(DownloadFileAsyncExtended);

            for (int i = 0; i <= ListViewEx.Items.Count - 1; i++)
            {
                //// Check if the Tag isn't Nothing, because else the download has already
                //// finished or an error occurred, so it can't be cancelled.
                if (ListViewEx.Items[i].Tag != null)
                {
                    //// Get the DownloadFileAsyncExtended class instance from the ListViewItem Tag.
                    wClient = (DownloadFileAsyncExtended)(ListViewEx.Items[i].Tag);
                    //// Cancel the download if it's still busy.
                    wClient.CancelAsync();
                }
            }
        }
예제 #4
0
        public void btnResumeAll_Click(System.Object sender, System.EventArgs e)
        {
            DownloadFileAsyncExtended wClient = default(DownloadFileAsyncExtended);

            for (int i = 0; i <= ListViewEx.Items.Count - 1; i++)
            {
                if (ListViewEx.Items[i].Tag != null)
                {
                    wClient = (DownloadFileAsyncExtended)(ListViewEx.Items[i].Tag);
                    //// Make sure you check if the download is not
                    //// already busy or an exception will be thrown.
                    if (wClient.IsBusy == false)
                    {
                        wClient.ResumeAsync();
                    }
                }
            }
        }
예제 #5
0
        public void StartDownload(string URL, string LocalFilePath)
        {
            DownloadFileAsyncExtended wClient = new DownloadFileAsyncExtended();

            //// Add some initial data to the Listview.
            ListViewItem lvw = this.Items.Add(Path.GetFileName(LocalFilePath));

            lvw.SubItems.Add("0 Bytes");         //// Total Size.
            lvw.SubItems.Add("Initializing..."); //// Status.
            lvw.SubItems.Add("0 Bytes");         //// Completed.
            lvw.SubItems.Add("0");               //// Progress Percentage. NOTE: Only add a number here, no letters/signs!
            lvw.SubItems.Add("0 kB/s");          //// Download Speed.
            lvw.SubItems.Add("00:00:00");        //// Download Time.
            lvw.SubItems.Add("00:00:00");        //// Remaining Time.
            lvw.ImageKey = "Initializing";       //// Initializing image.
            //// Store the DownloadFileAsyncExtended class instance in the Tag,
            //// so we can use it later to cancel/resume the download if necessary.
            lvw.Tag = wClient;

            //// Add Event handlers, so we can update the progress to the user.
            wClient.DownloadProgressChanged += DownloadProgressChanged;
            wClient.DownloadCompleted       += DownloadCompleted;

            //// IMPORTANT !!
            //// If you don't add this line, then all events are raised on a separate
            //// thread and you will get cross-thread errors when accessing the Listview
            //// or other controls directly in the raised events.
            wClient.SynchronizingObject = this;

            //// Update frequency. You can select NoDelay, HalfSecond or Second.
            //// HalfSecond and Second will prevent the DownloadProgressChanged event
            //// from firing continuously and hogging CPU when updating the controls.
            //// If you download small files that could be downloaded within a second,
            //// then set it to NoDelay or the progress might not be visible.
            wClient.ProgressUpdateFrequency = DownloadFileAsyncExtended.UpdateFrequency.Second;

            //// The method to actually download a file. The userToken parameter can
            //// for example be a control you wish to update in the DownloadProgressChanged
            //// and DownloadCompleted events. A ListViewItem in this example.
            wClient.DowloadFileAsync(URL, LocalFilePath, lvw);

            //// Set wClient to Nothing, because we don't need it anymore. Free storage
            wClient = null;
        }