Introduction
I am trying to make a WinForms
app using .Net
.
I am using tutorial from here which shows BackgroundWorker
and ProgressBar
integration.
I added ProgressBar
and BackgroundWorker
controls to the form.
The names are the same as in example. Additionally, I set WorkerReportProgress
property to True
for BackgroundWorker
. No errors are shown and the project compiles successfully.
Problem
The problem is - progressbar does not move.
And yet, it moves when clicked manually... progressBar1.PerformStep();
.
What am I missing?
Code
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, System.EventArgs e)
{
// Start the BackgroundWorker.
BackgroundWorker1.RunWorkerAsync();
}
private void BackgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 1; i <= 100; i++)
{
// Wait 500 milliseconds.
Thread.Sleep(500);
// Report progress.
BackgroundWorker1.ReportProgress(i);
}
}
private void BackgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
// Change the value of the ProgressBar to the BackgroundWorker progress.
progressBar1.Value = e.ProgressPercentage;
// Set the text.
this.Text = e.ProgressPercentage.ToString();
}
private void button2_Click(object sender, EventArgs e)
{
progressBar1.PerformStep();
}
}
}
Update
Removed progressBar1.PerformStep();
from DoWork
and ProgressChanged
.
Still the problem persists (ProgressBar does not move).
Thank you for ideas so far, will look into it more on Monday.
After you made sure you attached the event handlers to
ProgressChanged
andDoWork
:progressBar1.PerformStep()
fromDoWork
event handler.progressBar1.Value = e.ProgressPercentage;
inProgressChanged
event handler.