在foreach()中的C#进度条值 [英] C# Progressbar value in foreach()
问题描述
pre $ private $ button1_Click(object sender,EventArgs e)
{
var max = 0;
foreach(textBox2.Text.Split(',')中的var地址)
{
max = +1;
}
var maxp = 100 / max;
foreach(textBox2.Text.Split(',')中的var地址)
{
SendMessage(address);
progressBar1.Value = + maxp;
$ p
它计算文本框中有多少封电子邮件,然后做一个比例。给每个发送的邮件添加进度值,问题是当我按下按钮时,进度条不动。当所有电子邮件发送时,进度条移动到中风结束。
我该怎么办?
发生这种情况是因为循环和ui在同一个线程上执行。虽然循环繁忙,但无法更新ui
您可以使用 BackgroundWorker
来运行循环在后台的另一个线程中,然后使用BackgroundWorker的 ProgressChanged
事件更新您的进度条。您可以了解更多有关BackgroundWorkers 此处
I have this code:
private void button1_Click(object sender, EventArgs e)
{
var max = 0;
foreach (var address in textBox2.Text.Split(','))
{
max = +1;
}
var maxp = 100 / max;
foreach (var address in textBox2.Text.Split(','))
{
SendMessage(address);
progressBar1.Value = +maxp;
}
}
It calculates how many emails are in the textbox, and then makes a proportion. To each email sent adds the value of progress, the problem is that when I press the button, the progressbar does not move. When all emails are sent the progressbar moves to the end of stroke. How can I do?
This happens because you loop and the ui are executing on the same thread. While the loop is busy, it can't update the ui
You can use a BackgroundWorker
to run the loop on a different thread in the background, then use the BackgroundWorker's ProgressChanged
event to update your progressbar. You can learn more about BackgroundWorkers here
这篇关于在foreach()中的C#进度条值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!