FileCopy不会在进度条上显示复制的文件进度。它在复制操作完成时显示 [英] FileCopy doesnt showing copied file progress on progressbar.Its showing while copy operation is completed
本文介绍了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;
}
}
进度在运营结束时更新。
能否请您提出解决方案??
解决方案
为了执行文件复制并保持UI响应,你通常会想到多线程。但是你的实现有点远了;)
你的代码没有显示bw_DoWork()
。文件复制应该在那里进行。您不需要其他两个线程和Buffer-class。这太复杂了。只需要一个循环从一个文件中读取一大块字节并将其写入新文件,直到它到达文件末尾。
你不需要在BackgroundWorker的Eventhandler-Methods中调用,您不需要Application.DoEvents()。
有关使用BackgroundWorker的示例,请看一下:
http://www.dotnetperls.com/progressbar [ ^ ]
http://stackoverflow.com/a/1068743/4320056 [ ^ ]
http://www.codeproject.com/Messages/5050414/Re-WinForms-Update-A- Dialog.aspx [ ^ ]
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??
解决方案
You're generally on the right track with the idea of multi-threading in order to perform the file-copy and keeping your UI responsive. But your implementation is a bit far off ;)
Your code isn't showing what's happening inbw_DoWork()
. The file-copying should take place there. You don't need the other two threads and the Buffer-class. That's overcomplicating things. Just have a loop that reads from one file a chunk of bytes and writes it to the new file until it's reached the end of the file.
You don't need an Invoke in the Eventhandler-Methods of the BackgroundWorker and you don't need an Application.DoEvents().
For examples of using a BackgroundWorker please take a look here:
http://www.dotnetperls.com/progressbar[^]
http://stackoverflow.com/a/1068743/4320056[^]
http://www.codeproject.com/Messages/5050414/Re-WinForms-Update-A-Dialog.aspx[^]
这篇关于FileCopy不会在进度条上显示复制的文件进度。它在复制操作完成时显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文