backgroundWorker with ProgressChanged [英] backgroundWorker with ProgressChanged

查看:92
本文介绍了backgroundWorker with ProgressChanged的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在文件复制时增加进度条该代码有什么问题?





 < span class =code-keyword> private   void  btn_start_Click( object  sender,EventArgs e )
{
backgroundWorker1.RunWorkerAsync();


}

private void backgroundWorker1_DoWork( object sender,DoWorkEventArgs e)
{

string oldPathAndName = @ F:\ xp.flv;
string newPathAndName = @ E:\\ \\新电影\ xp.flv;
System.IO.File.Copy(oldPathAndName,newPathAndName);

}

private void backgroundWorker1_ProgressChanged(< span class =code-keyword> object sender,ProgressChangedEventArgs e)
{

pr.Value = e.ProgressPercentage;

}

解决方案

简单:您没有在DoWork方法中发出任何进展信号!系统无法计算出你有多远,所以它不适合你。

你可以通过DoWork方法调用来报告进度:

< pre lang =c#> backgroundWorker1.RepoertProgess(integerProgressValue);



如果是那里的代码,你无论如何也无法做到,因为一个文件.Copy操作无论如何都是阻塞操作 - 您调用该方法,并且在复制完成之前它不会返回。因此,当操作完成后,进度将立即从0%变为100%。



在这种情况下,你可能会更好地使用 Marquee样式进度条。


File.Copy()是一个同步操作,意味着它将阻止。



试试这个:如何使用可自定义的进度指示器和/或进度条在C#中复制文件[ ^ ]



编辑:

也看到了这一点: http:// stackoverflow .com / questions / 10517869 / update-progress-bar-while-copying-a-large-file [ ^ ]


替代解决方案

< a href =http://stackoverflow.com/questions/6687443/how-to-bring-up-the-built-in-file-copy-dialog> http://stackoverflow.com/questions/6687443/how - 启动内置文件复制对话框 [ ^ ]


How I Increase Progress bar while file copy what's wrong in that code?


private void btn_start_Click(object sender, EventArgs e)
        {
            backgroundWorker1.RunWorkerAsync();
            
            
        }
        
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
          
            string oldPathAndName = @"F:\xp.flv";
            string newPathAndName = @"E:\New Movies\xp.flv";
            System.IO.File.Copy(oldPathAndName, newPathAndName);

        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            
            pr.Value = e.ProgressPercentage;

        }

解决方案

Simple: you aren't signalling any progress in your DoWork method! The system can't work out how far you have got, so it won't do it for you.
You report progress by calling from your DoWork method:

backgroundWorker1.RepoertProgess(integerProgressValue);


In the case of the code you have there, you can't do it anyway, because a File.Copy operation is an blocking operation anyway - you call the method and it doesn't return until the copy is complete. So the progress would go from "0%" to "100%" immediately when the operation finished.

In this case, you would probably be better off using a "Marquee" style progress bar.


File.Copy() is a synchronous operation meaning that it will block.

Try this : How to copy files in C# with a customizable progress indicator and or progress bar[^]

EDIT:
see this also : http://stackoverflow.com/questions/10517869/update-progress-bar-while-copying-a-large-file[^]


alternative solution
http://stackoverflow.com/questions/6687443/how-to-bring-up-the-built-in-file-copy-dialog[^]


这篇关于backgroundWorker with ProgressChanged的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆