private void StartButton_Click(object sender, EventArgs e) { WaitTimer.Start(); CountdownThread.RunWorkerAsync(); StartButton.Enabled = false; StopButton.Enabled = true; }
private void StopButton_Click(object sender, EventArgs e) { CountdownThread.CancelAsync(); WaitTimer.Stop(); StopButton.Enabled = false; if (CountdownThread.IsBusy) { StartButton.Enabled = true; } }
private void CountdownThread_DoWork(object sender, DoWorkEventArgs e) { #region Variables string FROM; string TO; string Compression; decimal MAX; string MAX1; #endregion while (CountdownThread.CancellationPending == false) { FROM = XmlReader.GetWorld(); TO = XmlReader.GetBackupTo(); Compression = XmlReader.UseCompression(); MAX1 = XmlReader.GetBackupTime(); MAX = Convert.ToDecimal(MAX1); #region Do if variables not set if (FROM == "" || TO == "" || MAX == 0 || MAX1 == "") { while (CountdownThread.CancellationPending == false) { WaitTimer.Stop(); Log.MakeLog(null, "required things are blank: FROM: " + FROM + " TO: " + TO + " COMPRESSION:" + Compression + "TIMER: " + MAX.ToString()); this.Invoke((MethodInvoker) delegate() { StartButton.Enabled = true; }); this.Invoke((MethodInvoker) delegate() { StopButton.Enabled = false; }); CountdownThread.CancelAsync(); this.Invoke((MethodInvoker) delegate() { HelpLabel.Visible = true; }); e.Cancel = true; return; } } #endregion if (dec != 0.00M && !CountdownThread.CancellationPending) //reset value when thread runs// { dec = 0.00M; } #region Assign variables DirFrom = FROM; DirTo = TO; if (!DirFrom.EndsWith("\\") && DirFrom != "FTP") { DirFrom = DirFrom + "\\"; } if (!DirTo.EndsWith("\\") && DirTo != "FTP") { DirTo = DirTo + "\\"; } #endregion while (dec < MAX && !CountdownThread.CancellationPending) //Checks the time left is less than the total time { //do nothing// } DateNTime = DateTime.Now.ToString("MM.dd.yyyy hh-mm-ss"); #region Create directory and copy world if (Directory.Exists(DirTo) && DirTo != "FTP") { if (!Directory.Exists(DirTo + DateNTime)) //create the directory for the current time if it does not exist { Directory.CreateDirectory(DirTo + DateNTime); } if (DirFrom == "FTP") //if the from directory is FTP, start FTP downloading { Ftp_Download FTP = new Ftp_Download(); FTP.main(DateNTime); if (Compression == "yes") { CompressionBackground.RunWorkerAsync(); } } else if (DirTo == "FTP") { Ftp_Upload upload = new Ftp_Upload(); upload.Upload(DateNTime); } else if (DirFrom != "FTP") //if the from directory is not FTP, copy the directory. { #region create directories foreach (string dirPath in Directory.GetDirectories(DirFrom, "*", SearchOption.AllDirectories)) { Directory.CreateDirectory(dirPath.Replace(DirFrom, DirTo + DateNTime + "\\")); } foreach (string newPath in Directory.GetFiles(DirFrom, "*.*", SearchOption.AllDirectories)) { File.Copy(newPath, newPath.Replace(DirFrom, DirTo + DateNTime + "\\")); } #endregion if (Compression == "yes") { CompressionBackground.RunWorkerAsync(); } } else { //error } } else if (!Directory.Exists(DirTo) && DirTo != "FTP") { Directory.CreateDirectory(DirTo); if (DirFrom == "FTP") { Ftp_Download FTP = new Ftp_Download(); FTP.main(DateNTime); if (Compression == "yes") { CompressionBackground.RunWorkerAsync(); } } else { foreach (string CreateDir in Directory.GetDirectories(DirFrom, "*", SearchOption.AllDirectories)) { Directory.CreateDirectory(CreateDir.Replace(DirFrom, DirTo + DateNTime + "\\")); //create each sub directory } foreach (string file in Directory.GetFiles(DirFrom, "*", SearchOption.AllDirectories)) { File.Copy(file, file.Replace(DirFrom, DirTo + DateNTime + "\\")); //copy each file in sub directories, and in main directory } if (Compression == "yes") { CompressionBackground.RunWorkerAsync(); } } } else if (DirTo == "FTP") { Ftp_Upload Upload = new Ftp_Upload(); Upload.Upload(DateNTime); } else { //Error// MessageBox.Show("ERROR"); } #endregion } }