I’m learning winforms and I have set myself a simple goal of making a progressbar that goes from empty to full. Here’s my misshapen attempt:
public partial class Form1 : Form
{
static BackgroundWorker bw = new BackgroundWorker();
public Form1()
{
InitializeComponent();
bw.DoWork += bw_DoWork;
bw.RunWorkerAsync();
}
void bw_DoWork(object sender, DoWorkEventArgs e)
{
for(int i=0; i<100; ++i)
{
progressBar1.PerformStep();
Thread.Sleep(10);
}
}
}
I’m pretty sure that the Thread.Sleep() is reprehensible. How do I avoid it here?
You are already doing it almost right. The
BackgroundWorkerhas a built in mechanism for reporting progress already.Unless of course you’re just trying to animate a progress bar without actually reporting any progress, in which case you should probably just use the
Marqueetype that will automatically scroll a progressbar without doing anything. Or just use a background thread withThread.Sleep().