Example #1
0
        private void DownloadButton_Click(object sender, EventArgs e)
        {
            if (sender != null)
            {
                //This is a user call
                if (verifyTask != null && !verifyTask.IsCompleted)
                {
                    return;
                }

                if (downloadTask != null && !downloadTask.IsCompleted)
                {
                    return;
                }
            }

            if (filesAreInSync)
            {
                return;
            }

            if (filesThatRequireUpdate.Count > 0)
            {
                downloadTask = Task.Factory.StartNew((Action)(() =>
                {
                    //Reset the progress bar
                    Invoke(new MethodInvoker(() =>
                    {
                        progressBar1.Visible = false;
                        progressBar1.Value = 0;
                        progressBar1.Maximum = 1;
                    }));

                    var filesToDownload = filesThatRequireUpdate.Where(f => f.RemoteFileInfo != null);
                    var totalFilesToDownload = filesToDownload.Count();
                    var totalKilobytesToDownload = filesToDownload.Sum(f => f.RemoteFileInfo.Length) / 1024d;

                    Invoke(new MethodInvoker(() =>
                    {
                        progressBar1.Visible = true;
                        progressBar1.ForeColor = SystemColors.Highlight;
                        progressBar1.Maximum = (int)totalKilobytesToDownload;
                    }));

                    int downloadCount = 0;

                    var ftpDownloader = new FtpDownloader();
                    ftpDownloader.OnProgressChanged += new FtpDownloader.ProgressChangedSignature((bytes) =>
                    {
                        var kb = bytes / 1024d;
                        Invoke(new MethodInvoker(() =>
                        {
                            progressBar1.Value = (int)kb;
                        }));
                    });

                    ftpDownloader.OnStartDownload += new FtpDownloader.StartDownloadSignature((pair) =>
                    {
                        SetCurrentAction("Downloading " + Path.GetFileName(pair.LocalFilename));
                    });

                    ftpDownloader.OnFinishedDownload += new FtpDownloader.FinishedDownloadSignature((pair) =>
                    {
                        Interlocked.Increment(ref downloadCount);
                    });

                    bool allDownloadedSuccessfully = ftpDownloader.DownloadFiles(filesToDownload, settings.DownloadThreads);

                    Invoke(new MethodInvoker(() =>
                    {
                        progressBar1.Value = progressBar1.Maximum;
                    }));


                    if (!allDownloadedSuccessfully)
                    {
                        MessageBox.Show("Possible FTP Connection Problem" + Environment.NewLine + "Not all files were downloaded." + Environment.NewLine + "Please verify and try again.",
                                        "FTP Download Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }

                    var filesToDelete = filesThatRequireUpdate.Where(f => f.RemoteFileInfo == null).ToList();
                    filesToDelete.ForEach(pair =>
                    {
                        SetCurrentAction("Removing file " + Path.GetDirectoryName(pair.LocalFilename) + @"\" + Path.GetFileNameWithoutExtension(pair.LocalFilename));
                        File.Delete(pair.LocalFilename);
                    });

                    SetCurrentAction(string.Format("Finished. Downloaded {0:N0} files.", downloadCount));

                    filesAreInSync = true;
                    updateStatus.BackgroundImage = Properties.Resources.green_light;

                    //Reset the progress bar
                    Invoke(new MethodInvoker(() =>
                    {
                        progressBar1.Visible = false;
                        progressBar1.Value = 0;
                    }));

                    if (settings.AutomaticallyBuildLinksAfterDownload)
                    {
                        Rebuildbutton_Click(null, null);
                    }

                    filesThatRequireUpdate.Clear();
                }));
            }
        }