c#-我正在尝试使用backgroundworker,但是它不起作用-为什么?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c#-我正在尝试使用backgroundworker,但是它不起作用-为什么?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2876字,纯文字阅读大概需要5分钟。
内容图文
这是DoWork事件ProgressChanged事件和RunWorkerCompleted事件的代码.问题是在函数处理操作结束之前,progressBar的速度要快100%.因此,当progressBar达到100%时,我会收到错误异常,因为progressBar不能为101%
我需要以某种方式使progressBar根据功能进程/进度获得进度.我猜我在DoWork事件中的计算有问题.
在Form1顶部,我添加了:
Int i;
在构造函数中,我做到了:
i = 0;
backgroundWorker1.WorkerSupportsCancellation = true;
backgroundWorker1.WorkerReportsProgress = true;
backgroundWorker1.RunWorkerAsync();
这是Backgroundworker的事件:
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
//int currentLength;
//int currentIndex;
//int lastIndex = 0;
string startTag = "T256=\"";
string endTag = "\"";
int startTagWidth = startTag.Length;
//int endTagWidth = endTag.Length;
int index = 0;
h = new StreamReader(@"d:\DeponiaSplit\testingdeponias_Translated.txt");
while ((line = h.ReadLine()) != null)
{
if (index > f.LastIndexOf(startTag))
{
break;
}
int startTagIndex = f.IndexOf(startTag, index);
int stringIndex = startTagIndex + startTagWidth;
index = stringIndex;
int endTagIndex = f.IndexOf(endTag, index);
int stringLength = endTagIndex - stringIndex;
if (stringLength != 0)
{
string test = f.Substring(stringIndex, stringLength);
f = f.Substring(0, stringIndex) + line + f.Substring(stringIndex + stringLength);
if (listBox1.InvokeRequired)
{
textBox1.Invoke(new MethodInvoker(delegate { textBox1.Text = line; }));
}
i = i + 1;
System.Threading.Thread.Sleep(500);
worker.ReportProgress((i * 1));
}
}
h.Close();
StreamWriter w = new StreamWriter(@"D:\New folder (24)\000004aa.xml");
w.AutoFlush = true;
w.Write(f);
w.Close();
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
//this.progressBar1.Text = (e.ProgressPercentage.ToString() + "%");
progressBar1.Value = e.ProgressPercentage;
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if ((e.Cancelled == true))
{
this.progressBar1.Text = "Canceled!";
}
else if (!(e.Error == null))
{
this.progressBar1.Text = ("Error: " + e.Error.Message);
}
else
{
this.progressBar1.Text = "Done!";
}
}
为什么不能正常工作?
使用FileStream
现在,我想在ProgressBar上添加一个标签,该标签将显示%,并且该标签将根据progressBar移动.我不想禁用ProgressBar绿色进度,而只是添加%并以百分比显示数字.
所以在ProgressChanged事件中,我做了:
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
label3.Text = (e.ProgressPercentage.ToString() + "%");
}
但这不起作用-label13不会仅将%更改为1.我想看到类似1%… 2%… 3%…之类的内容.我该怎么做?
解决方法:
为什么不使用File.ReadAllLines,它返回一个字符串数组.您可以将进度报告为正在处理的行占总行数的百分比:
string[] lines = File.ReadAllLines(@"d:\DeponiaSplit\testingdeponias_Translated.txt");
// ...
worker.ReportProgress(i * 100 / lines.Length);
内容总结
以上是互联网集市为您收集整理的c#-我正在尝试使用backgroundworker,但是它不起作用-为什么?全部内容,希望文章能够帮你解决c#-我正在尝试使用backgroundworker,但是它不起作用-为什么?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。