它在复制操作完成时显示

它在复制操作完成时显示

本文介绍了FileCopy不会在进度条上显示复制的文件进度。它在复制操作完成时显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有应用文件的Windows应用程序,进度条显示复制的进度。但是无法更新进度条。当复制操作完成时它会更新。



  public   partial   class  FileCopy:Form 
{
private BackgroundWorker bw = new BackgroundWorker( );
public FileCopy()
{
InitializeComponent();
bw.WorkerReportsProgress = true ;
bw.WorkerSupportsCancellation = true ;
bw.DoWork + = new DoWorkEventHandler(bw_DoWork);
bw.ProgressChanged + = new ProgressChangedEventHandler(bw_ProgressChanged);
bw.RunWorkerCompleted + = new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);

}
onstartButtonClick()
{
线程readerThread = 线程( new ThreadStart(readData));
readerThread.Start();
线程writerThread = new 线程( new ThreadStart(writeData));
writerThread.Start();
writerThread.Join();
readerThread.Join();
}
readData()
{
while (length > 0
{
data = br.ReadBytes(data.Length);
Buffer.putData(data);
length = length - data.Length;
Console.WriteLine( 读取{0},data.Length);
bw.ReportProgress( 10 );
}
Buffer.STOP = false ;
Buffer._reset.Set();
}
private void bw_ProgressChanged( object sender,ProgressChangedEventArgs e)
{
this .BeginInvoke( new 操作(()= >
{
Console.WriteLine( 内部开始调用);
progressBar1.Value = e.ProgressPercentage;
Application.DoEvents();
}) );

}
public void writeData()
{
while (Buffer.STOP)
{
if ( Buffer.STOP!= false
{
Buffer._reset.WaitOne();
byte [] data = Buffer.takeData();
if (data!= null && Buffer.STOP == true
{
fs.Write(data, 0 ,data.Length);
fs.Flush();
}
Console.WriteLine( 写入文件: +数据。长度);
Buffer._reset.Reset();
}
}
}
}
静态 class 缓冲区
{
public static byte [] _data = new byte [ 10240 ];
public static AutoResetEvent _reset = new AutoResetEvent( false );
私有 静态 bool _isStop = true ;

public static bool STOP
{
get
{
return _isStop;
}
set
{
_isStop = value ;
}
}
public static void putData( byte [] data)
{
_data = data;
Console.WriteLine( 数据长度: + data.Length);
_reset.Set();
}
public static byte [] takeData()
{
return _data;
}
}



进度在运营结束时更新。

能否请您提出解决方案??

解决方案

I have windows application for coping file with progress bar showing copied progress.But could not update progress bar.Its get updated when copy operation is get completed.

public partial class FileCopy : Form
{
  private BackgroundWorker bw = new BackgroundWorker();
 public FileCopy()
       {
           InitializeComponent();
           bw.WorkerReportsProgress = true;
           bw.WorkerSupportsCancellation = true;
           bw.DoWork += new DoWorkEventHandler(bw_DoWork);
           bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
           bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);

       }
 onstartButtonClick()
 {
   Thread readerThread = new Thread(new ThreadStart(readData));
   readerThread.Start();
   Thread writerThread = new Thread(new ThreadStart(writeData));
   writerThread.Start();
   writerThread.Join();
   readerThread.Join();
 }
 readData()
 {
   while (length > 0)
   {
    data = br.ReadBytes(data.Length);
    Buffer.putData(data);
    length = length - data.Length;
    Console.WriteLine("Reading {0}", data.Length);
    bw.ReportProgress(10);
   }
    Buffer.STOP = false;
    Buffer._reset.Set();
 }
private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
       {
           this.BeginInvoke(new Action(() =>
           {
               Console.WriteLine("Inside Begin Invoke");
               progressBar1.Value = e.ProgressPercentage;
               Application.DoEvents();
           }));

       }
 public void writeData()
 {
    while (Buffer.STOP)
   {
     if (Buffer.STOP != false)
     {
          Buffer._reset.WaitOne();
          byte[] data = Buffer.takeData();
          if (data != null && Buffer.STOP == true)
          {
            fs.Write(data, 0, data.Length);
            fs.Flush();
          }
          Console.WriteLine("Writing to file :" + data.Length);
          Buffer._reset.Reset();
       }
   }
  }
}
 static class Buffer
   {
       public static  byte[] _data = new byte[10240];
       public static AutoResetEvent _reset = new AutoResetEvent(false);
       private static bool _isStop = true;

       public static bool STOP
       {
           get
           {
               return _isStop;
           }
           set
           {
               _isStop = value;
           }
       }
       public static void putData(byte[] data)
       {
           _data = data;
           Console.WriteLine("Data Length :" + data.Length);
           _reset.Set();
      }
       public static byte[] takeData()
       {
           return _data;
       }
   }


Progress is updated at end of operation.
Can you please suggest solution??

解决方案


这篇关于FileCopy不会在进度条上显示复制的文件进度。它在复制操作完成时显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 23:40