private Task downloadFile(string sourceIP, string sourcePath, string destinationPath) { return(Task.Factory.StartNew(() => { bool isDone = false; FileDownloadManagement download = new FileDownloadManagement(); download.SourceIp = sourceIP; TransferDetails info = new TransferDetails(sourcePath, destinationPath); download.fileDetails.Add(info); download.AllFileDownloadingCompletedCallback = new FileDownloadManagement.AllFileDownloadingCompleted((FileDownloadManagement sender, RServer server) => { isDone = true; }); download.StartDownloading(); while (!isDone) { Thread.Sleep(10); } })); }
private void downloadFiles(List <r.FileInformationResponse.FileInfo> files, string destinationPath) { FileDownloadManagement download = new FileDownloadManagement(); //IP download.SourceIp = server.serverPort.ServerIP; //PROGRESS FORM TransferProgressBoxForm pBox = new TransferProgressBoxForm(); //FILES foreach (r.FileInformationResponse.FileInfo sourceFile in files) { TransferDetails details = new TransferDetails(); //SOURCE PATH details.SourcePath = sourceFile.FullName; //DESTINATION PATH if (destinationPath.EndsWith("\\")) { details.DestinationPath = destinationPath + sourceFile.Name; } else { details.DestinationPath = $"{destinationPath}\\{sourceFile.Name}"; } //LENGTH details.FileLength = sourceFile.Length; download.TotalSizeForTransfer += details.FileLength; //UI ATTACHMENT //FIRST TAG ListViewItem liv = new ListViewItem($"{server.nickName} - [{download.SourceIp}]"); liv.SubItems.Add("Waiting..."); liv.SubItems.Add("00:00:00"); liv.SubItems.Add(details.SourcePath); liv.SubItems.Add(details.DestinationPath); transferContentView.Items.Add(liv); details.Tag.Add(liv); //TAG //SECOND TAG ListViewItem l = new ListViewItem(details.DestinationPath); pBox.addListViewItems(l); details.Tag.Add(l); //TAG //ADD TO MASTER download.fileDetails.Add(details); } transferContentView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent); //STARTED CALLBACK download.FileDownloadingStartedCallback = new FileDownloadManagement.FileDownloadingStarted( (FileDownloadManagement sender, TransferDetails objectDetails) => { Invoke(new UpdateData(() => { try { pBox.singleItemStarting((ListViewItem)objectDetails.Tag[1]); } catch (Exception) { } })); }); //PROGRESS CALLBACK download.DownloadingProgressCallback = new FileDownloadManagement.FileDownloadingProgress( (FileDownloadManagement sender, TransferDetails objectDetails, double progressRatio, Stopwatch Timer) => { Invoke(new UpdateData(() => { ListViewItem liv = (ListViewItem)objectDetails.Tag[0]; liv.SubItems[1].Text = $"{(progressRatio * 100).ToString("00.00")}%"; liv.SubItems[2].Text = $"{Timer.Elapsed.ToString("hh\\:mm\\:ss")}"; try { pBox.progressNotification((int)(sender.ActualSizeTransfered / sender.TotalSizeForTransfer * 100)); } catch (Exception) { } })); }); //COMPLETION CALLBACK download.DownloadingCompletedCallback = new FileDownloadManagement.FileDownloadingCompleted( (FileDownloadManagement sender, TransferDetails objectDetails, Stopwatch Timer) => { ListViewItem liv = (ListViewItem)objectDetails.Tag[0]; Invoke(new UpdateData(() => { liv.ForeColor = Color.Green; liv.SubItems[2].Text = $"{Timer.Elapsed.ToString("hh\\:mm\\:ss")}"; try { pBox.singleItemCompletion((ListViewItem)objectDetails.Tag[1]); } catch (Exception) { } })); }); //ALL TRANSFER COMPLETED CALLBACK download.AllFileDownloadingCompletedCallback = new FileDownloadManagement.AllFileDownloadingCompleted( (FileDownloadManagement sender, RServer server) => { Invoke(new UpdateData(() => { try { pBox.Close(); } catch (Exception) { } try { pBox.Dispose(); } catch (Exception) { } })); } ); //ERROR CALLBACK download.ConnectionEstablishErrorCallback = new FileDownloadManagement.ConnectionEstablishError( (FileDownloadManagement sender, RServer server, string message) => { foreach (TransferDetails item in sender.fileDetails) { ListViewItem liv = (ListViewItem)item.Tag[0]; Invoke(new UpdateData(() => { liv.SubItems[1].Text = message; liv.ForeColor = Color.Red; })); } } ); download.skeepCallback(100); //REDUCE CALLBACKS //SHOW FORM pBox.TotalNumberOfItems = files.Count; pBox.senderIP = server.serverPort.ServerIP; pBox.receiverIP = download.DestinationIp; pBox.progressNotification(0); pBox.Show(); //START DOWNLOADING download.StartDownloading(); }