private void handleTransferStateChanged(object sender, TransferStateChangedEventArgs e) { TransferInfo t = (TransferInfo)sender; if (t.IsSending && e.NewState == TransferState.InProgress) { // we just started sending an app to a peer updateStatus(String.Format("Sending {0} to {1}", t.App.Name, t.Peer.Hostname)); } else if (!(t.IsSending) && e.NewState == TransferState.Complete) { // we just finished receiving an app from a peer updateStatus(String.Format("Received {0} from {1}", t.App.Name, t.Peer.Hostname)); if (!Library.Apps.ContainsKey(t.App.AppId)) { Library.Apps.Add(t.App.AppId, t.App); } updateAvailableApps(); } // see if we have anything on the queue that we should start startQueuedTransfers(); }
private void transferStateChanged(object sender, TransferStateChangedEventArgs e) { if (this.InvokeRequired) { TransferStateChangedEventHandler d = new TransferStateChangedEventHandler(transferStateChanged); this.Invoke(d, new object[] { sender, e }); } else { // ignore if we're closing (i.e. exiting app) if (_isClosing) { return; } TransferInfo transfer = (TransferInfo)sender; if (e.NewState == TransferState.Failed) { // show message box with failure reason labelTransferName.Text = ""; labelTransferTime.Text = ""; labelTransferSpeed.Text = ""; string msg = "Unable to sync " + transfer.App.Name + "."; if (e.Reason.Length > 0) { msg += " " + e.Reason + "."; } MessageBox.Show(msg, "Transfer Failed"); } else if (e.NewState == TransferState.WaitingForManifest && !transfer.IsSending) { labelTransferName.Text = transfer.App.Name; labelTransferTime.Text = "Starting..."; } else if (e.NewState == TransferState.InProgress && !transfer.IsSending) { progressBarTransfer.Maximum = (int)(transfer.TotalBytes / 1024); progressBarTransfer.Value = (int)(transfer.BytesTransferred / 1024); labelTransferName.Text = transfer.App.Name; } else if (e.NewState == TransferState.Complete && !transfer.IsSending) { progressBarTransfer.Maximum = 10; progressBarTransfer.Value = 10; long bytesPerSec = transfer.TransferSpeed; TimeSpan timeTaken = TimeSpan.FromSeconds(transfer.ElapsedMilliseconds / 1000); labelTransferTime.Text = String.Format(new FileSizeFormatProvider(), "{0:fs} in ", transfer.TotalBytes) + timeTaken.ToString("c"); labelTransferSpeed.Text = String.Format(new FileSizeFormatProvider(), "{0:fs}/s", bytesPerSec); // todo - do this in a way that doesn't block the UI thread MessageBox.Show(String.Format("{0} has finished syncing!", transfer.App.Name), "Transfer Complete"); } } }