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 your 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'sProgressChanged
event to update your progressbar. You can learn more about BackgroundWorkers here