private void FtpUploadListForm_FormClosing(object sender, FormClosingEventArgs e) { FtpDownUploadFileUserControl ctr = ( from x in ftpListUserControl1.lboxTask.Items.OfType <FtpDownUploadFileUserControl>() where x.IsRunning == true || x.IsWaitting == true select x ).FirstOrDefault(); if (ctr != null) { MessageBoxButtons messButton = MessageBoxButtons.OKCancel; DialogResult dr = MessageBox.Show("有文件正在上传,确定退出?", "系统提示", messButton); //退出 if (dr == DialogResult.OK) { e.Cancel = false; } //取消退出 else { e.Cancel = true; } } }
/// <summary> /// 控件加载处理 /// </summary> private void FtpListUserControl_Loaded(object sender, RoutedEventArgs e) { System.Timers.Timer timer = new System.Timers.Timer(); timer.AutoReset = true; timer.Enabled = true; timer.Interval = 1000; timer.Elapsed += (s, ex) => { this.Dispatcher.Invoke(new Action(() => { FtpDownUploadFileUserControl ctr = ( from x in lboxTask.Items.OfType <FtpDownUploadFileUserControl>() where x.IsRunning == true select x ).FirstOrDefault(); if (ctr == null) { ctr = ( from x in lboxTask.Items.OfType <FtpDownUploadFileUserControl>() where x.IsRunning == false && x.IsWaitting == true && x.IsCanclled == false && x.IsFailed == false select x ).FirstOrDefault(); if (ctr != null) { ctr.Run(); } } })); }; }
//全部取消 private void btnCancel_Click(object sender, RoutedEventArgs e) { for (int i = 0; i < lboxTask.Items.Count; i++) { FtpDownUploadFileUserControl ctr = lboxTask.Items[i] as FtpDownUploadFileUserControl; if (!ctr.IsCanclled && !ctr.IsFailed) { ctr.btnCancel.RaiseEvent(new RoutedEventArgs(Button.ClickEvent)); } } }
//全部暂停 private void btnPause_Click(object sender, RoutedEventArgs e) { for (int i = 0; i < lboxTask.Items.Count; i++) { FtpDownUploadFileUserControl ctr = lboxTask.Items[i] as FtpDownUploadFileUserControl; if (ctr.IsRunning || ctr.IsWaitting) { ctr.btnRun.RaiseEvent(new RoutedEventArgs(Button.ClickEvent)); } } }
/// <summary> /// 添加任务 /// </summary> public void Add(FileInfoEntity info) { FtpDownUploadFileUserControl ctr = new FtpDownUploadFileUserControl(info); ctr.Cancelled += (s, ex) => { _Sum--; RefreshUI(); }; ctr.Cancelled += Cancelled; ctr.Failed += Failed; ctr.Done += (s, ex) => { this.Dispatcher.Invoke(() => { lboxTask.Items.Remove(ctr); if (lboxTask.Items.Count == 0) { Task.Factory.StartNew(() => { System.Threading.Thread.Sleep(2000); this.Dispatcher.Invoke(() => { listForm.Close(); }); }); } }); _Done++; RefreshUI(); }; ctr.Done += Done; ctr.RegisterEvent(); lboxTask.Items.Add(ctr); _Sum++; RefreshUI(); }