日期:2014-05-18  浏览次数:20934 次

本人新手求教多线程问题!这是一段代码,当文件传输完毕怎么用线程打开第二个窗口!
public partial class MainWindow : Window
  {
  Thread copyThread;
  public MainWindow()
  {
  InitializeComponent();
  }


  private void button2_Click(object sender, RoutedEventArgs e)
  {
  SaveFileDialog saveFile = new SaveFileDialog();
  saveFile.AddExtension = true;
  saveFile.Filter = "*.rar|*.rar|all files|*.*";
  saveFile.FilterIndex = 0;
  bool? f = saveFile.ShowDialog();
  if (f != null && f.Value)
  {
  this.saveFilePath.Text = saveFile.FileName;
  }
  }

  private void button4_Click(object sender, RoutedEventArgs e)
  {
  string fileName = this.srcFile.Text.Trim();
  string destPath = this.saveFilePath.Text.Trim();
  if(!File.Exists(fileName))
  {
  MessageBox.Show("源文件不存在");
  return; 
  }  
  this.copyFlag.Text = "开始复制。。。"; 
  this.copyProgress.Maximum = (new FileInfo(fileName)).Length;  
  CopyFileInfo c = new CopyFileInfo() 
  {
  SourcePath = fileName, DestPath = destPath 
  };
  copyThread = new Thread(new ParameterizedThreadStart(CopyFile));  
  copyThread.Start(c);  
  this.copyFlag.Text = "复制完成。。。";
   
  }
  private void CopyFile(object obj)
  {
  CopyFileInfo c = obj as CopyFileInfo;  
  CopyFile(c.SourcePath, c.DestPath);
  }
  private void CopyFile( string sourcePath,string destPath)
  {
  FileInfo f = new FileInfo(sourcePath);
  FileStream fsR = f.OpenRead();
  FileStream fsW = File.Create(destPath);
  long fileLength = f.Length;
  byte[] buffer = new byte[1024];
  int n = 0;  
  while (true)
  {  
  this.displayCopyInfo.Dispatcher.BeginInvoke(DispatcherPriority.SystemIdle,
  new Action<long, long>(UpdateCopyProgress), fileLength, fsR.Position);  
  n=fsR.Read(buffer, 0, 1024);
  if (n==0)
  {
  break;
  }
  fsW.Write(buffer, 0, n);
  fsW.Flush();
  Thread.Sleep(1);
  }  
  fsR.Close();
  fsW.Close(); 
  }
  private void UpdateCopyProgress(long fileLength,long currentLength)
  {
  this.displayCopyInfo.Text = string.Format("总大小:{0},已复制:{1}", fileLength, currentLength);  
  this.copyProgress.Value = currentLength;
   
  }

  private void Window_Closed(object sender, EventArgs e)
  {
  //关闭启用的线程
  copyThread.Abort();<