Winfrom进度条怎样 与项目的时间同步
我做了一个读取条形码的项目,会显示读取一个图片的时间,想加上一个进度条, 但不知道怎样让进度条和读取的时间同步,现在我的进度条只会从1显示到100,没有和读取时间同步,求高手解决,下面贴进度条的代码public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//用子线程工作
new System.Threading.Thread(new System.Threading.ThreadStart(StartDownload)).Start();
}
//开始下载
public void StartDownload()
{
Downloader downloader = new Downloader();
downloader.onDownLoadProgress += new Downloader.dDownloadProgress(downloader_onDownLoadProgress);
downloader.Start();
}
//同步更新UI
void downloader_onDownLoadProgress(long total, long current)
{
if (this.InvokeRequired)
{
this.Invoke(new Downloader.dDownloadProgress(downloader_onDownLoadProgress), new object[] { total, current });
}
else
{
this.progressBar1.Maximum = (int)total;
this.progressBar1.Value = (int)current;
}
}
}
/// <summary>
/// 下载类()
/// </summary>
public class Downloader
{
//委托
public delegate void dDownloadProgress(long total,long current);
//事件
public event dDownloadProgress onDownLoadProgress;
//开始模拟工作
public void Start()
{
for (int i = 0; i < 100; i++)
{
if (onDownLoadProgress != null)
onDownLoadProgress(100, i);
System.Threading.Thread.Sleep(100);
}
}
}
}
--------------------编程问答-------------------- 多了解了解时间函数! --------------------编程问答-------------------- 帮顶 --------------------编程问答-------------------- 来个高手 帮忙啊 --------------------编程问答-------------------- 你要使你的进度条能显示你得读取进度,那你首先得能知道你读取总共所需时间,然后需要在读取内部还要通知进度条我进行到哪了,比如总共时间为10s,你进度条分为10个等分,读取时你每1s通知进度条progress一下.我估计你是很难把读取分时间片的。 --------------------编程问答-------------------- 问题是读取图片的时间不确定,也不知道会读取多长时间,要根据读取的时间设置progressBar1的最大值,进行同步。那怎么弄呀。帮你顶,望高手出招。学习! --------------------编程问答--------------------
可以根据读取时间的百分比进行同步。 --------------------编程问答-------------------- 进度条和时间同步,这个东西貌似不能把。因为好多因素都是不确定的!当然你可以估算一下,读取一张图片的时间,然后在设置进度条的时间。(图片读取貌似不确定把) --------------------编程问答-------------------- 在读文件那里更新进度,读了多少字节就更新多少进度。
补充:.NET技术 , C#