void stopBtn_Click(object sender, RoutedEventArgs e) { if (stopDict.ContainsKey((System.Windows.Controls.Button)sender)) { DownLoadInfo dInfo = stopDict[(System.Windows.Controls.Button)sender]; if (!dInfo.Complete) { if ((dInfo.Thread.ThreadState & ThreadState.Suspended) != 0) { dInfo.Thread.Resume(); } dInfo.Thread.Abort(); dInfo.Complete = true; pauseDict.Remove((System.Windows.Controls.Button)sender); stopDict.Remove((System.Windows.Controls.Button)sender); } else { pauseDict.Remove((System.Windows.Controls.Button)sender); stopDict.Remove((System.Windows.Controls.Button)sender); } } }
void pauseBtn_Click(object sender, RoutedEventArgs e) { if (pauseDict.ContainsKey((System.Windows.Controls.Button)sender)) { DownLoadInfo dInfo = pauseDict[(System.Windows.Controls.Button)sender]; if (!dInfo.Complete) { Console.WriteLine(dInfo.Thread.ThreadState.ToString()); if ((dInfo.Thread.ThreadState & ThreadState.Suspended) != 0) { dInfo.Thread.Resume(); dInfo.ProgressBar.Foreground = Brushes.Green; } else { dInfo.Thread.Suspend(); dInfo.ProgressBar.Foreground = Brushes.Yellow; } } else { pauseDict.Remove((System.Windows.Controls.Button)sender); stopDict.Remove((System.Windows.Controls.Button)sender); } } }
void OpenReadCompletedGeneral(int num, long size, Stream stream) { string pathToCopy = string.Concat(pathToCopyTemp, "\\", fileList[num]); //Console.WriteLine(pathToCopy); if (!File.Exists(pathToCopy)) { System.Windows.Controls.Label label = new System.Windows.Controls.Label(); label.Content = fileList[num]; label.Height = 30; label.Width = 90; label.Margin = new Thickness(5); label.ToolTip = pathToCopy; wrapPanel.Children.Add(label); System.Windows.Controls.ProgressBar pB = new System.Windows.Controls.ProgressBar(); pB.Height = 20; pB.Width = 350; pB.Margin = new Thickness(5); wrapPanel.Children.Add(pB); System.Windows.Controls.Button pauseBtn = new System.Windows.Controls.Button(); pauseBtn.Content = "||"; pauseBtn.Height = pauseBtn.Width = 20; pauseBtn.Margin = new Thickness(5); pauseBtn.Click += pauseBtn_Click; wrapPanel.Children.Add(pauseBtn); System.Windows.Controls.Button stopBtn = new System.Windows.Controls.Button(); stopBtn.Content = "[]"; stopBtn.Height = stopBtn.Width = 20; stopBtn.Margin = new Thickness(5); stopBtn.Click += stopBtn_Click; wrapPanel.Children.Add(stopBtn); Thread thread = new Thread(DownloadInThread); thread.IsBackground = true; DownLoadInfo downInfo = new DownLoadInfo(thread, pathToCopy, pB, stream, label, size); pauseDict.Add(pauseBtn, downInfo); stopDict.Add(stopBtn, downInfo); thread.Start(downInfo); } else { System.Windows.MessageBox.Show(pathToCopy + " уже существует"); } }
void DownloadInThread(object dInfos) { int kbCount = 0; int part = 1024; DownLoadInfo dInfo = dInfos as DownLoadInfo; Stream stream = dInfo.Stream; FileStream fs = new FileStream(dInfo.Path, FileMode.Create); //FileStream fs = new FileStream("E:\\ubuntu-14.04.1-desktop-amd64.iso\0\0\0", FileMode.Create); byte[] buffer = new byte[part]; int bytesCount = 0; try { while ((bytesCount = stream.Read(buffer, 0, part)) != 0) { kbCount++; //Console.WriteLine(dInfo.TotalSize.ToString()); dInfo.ProgressBar.Dispatcher.Invoke(new Action(() => { //dInfo.ProgressBar.Value = kbCount * 100 / (dInfo.TotalSize / 1024); })); fs.Write(buffer, 0, bytesCount); } //Console.WriteLine(dInfo.Path + " completed"); dInfo.Complete = true; stream.Close(); fs.Close(); } catch (ThreadAbortException exp) { //Console.WriteLine("Словил " + exp.Message); stream.Close(); fs.Close(); File.Delete(dInfo.Path); dInfo.Complete = true; dInfo.ProgressBar.Dispatcher.Invoke(new Action(() => { dInfo.ProgressBar.Foreground = Brushes.Red; })); } }