Example #1
0
        private void SyncTaskAction(object state)
        {
            bool canceled = false;

            try
            {
                var startTime = DateTimeOffset.Now;

                var cancelToken = (CancellationToken)state;

                var localRootDir = new DirectoryInfo(this.Session.DirectoryToSync);

                IList <DirectoryInfo> allDirs = new SynchronizedCollection <DirectoryInfo>();
                if (this.Recursive)
                {
                    CollectDirectories(localRootDir, allDirs);
                }

                this.ProgressBar_Directories
                .InvokeSafe((pb, pbState) =>
                {
                    pb.Minimum = 0;
                    pb.Maximum = pbState.DirectoryList.Count;

                    pb.Value = 0;
                }, new
                {
                    DirectoryList = allDirs,
                });

                var linesLeft      = allDirs.Count;
                var linesProcessed = 0;

                foreach (var localDir in allDirs.OrderBy(d => d.FullName.Length)
                         .ThenBy(d => d.Name, StringComparer.InvariantCultureIgnoreCase))
                {
                    if (cancelToken.IsCancellationRequested)
                    {
                        canceled = true;
                        break;
                    }

                    var remotePath = this.Session.ToServerFilePath(localDir.FullName);
                    var remoteDir  = this.Session.Server.FileSystem.ListDirectory(remotePath);

                    var labelTxt = string.Format("{0} => {1}",
                                                 localDir.FullName,
                                                 remotePath);

                    this.Label_Directories
                    .InvokeSafe((lbl, lblState) =>
                    {
                        lbl.Text = lblState.Text;
                        lblState.ToolTip.SetToolTip(lbl, lblState.Text);
                    }, new
                    {
                        Text    = labelTxt,
                        ToolTip = this.ToolTip_Label_Directories,
                    });

                    this.SyncDirectory(localDir, remoteDir,
                                       cancelToken);

                    var now       = DateTimeOffset.Now;
                    var timeTaken = now - startTime;

                    var secondsLeft = (timeTaken.TotalSeconds / (double)++linesProcessed) *
                                      (double)--linesLeft;

                    this.Label_RemainingTime
                    .InvokeSafe((lbl, lblState) =>
                    {
                        lbl.Text = string.Format("Remaining time: about {0:hh}:{0:mm}:{0:ss}",
                                                 lblState.TimeLeft);
                    }, new
                    {
                        TimeLeft = TimeSpan.FromSeconds(secondsLeft),
                    });

                    this.ProgressBar_Directories
                    .InvokeSafe((pb) =>
                    {
                        pb.Increment(1);
                    });
                }

                this.Label_RemainingTime
                .InvokeSafe((lbl, lblState) =>
                {
                    lbl.Text = lblState.Canceled ? "Canceled" : "Done";
                }, new
                {
                    Canceled = canceled,
                });
            }
            catch (Exception ex)
            {
                canceled = true;

                this.InvokeSafe((form, frmState) =>
                {
                    MessageBox.Show(form,
                                    frmState.Exception.ToString(),
                                    frmState.Exception.GetType().FullName,
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Error);
                }, new
                {
                    Exception = ex.GetBaseException() ?? ex,
                });
            }
            finally
            {
                this.CurrentSyncTask   = null;
                this.SyncTaskCanceller = null;

                this.InvokeSafe((form, frmState) =>
                {
                    form.DialogResult = frmState.Canceled ? DialogResult.Cancel : DialogResult.OK;
                }, new
                {
                    Canceled = canceled,
                });
            }
        }