I'm trying to create a simple progress bar to update using a thread on my form. I can't seem to get the Invoke call to fire on the progress bar, can anyone tell me why?
Here is my very simple form, with just a progress bar and button to execute.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//vars
System.Threading.Thread thr = null;
bool runProgressBarUpdateThread = false;
private void button1_Click(object sender, EventArgs e)
{
thr = new System.Threading.Thread(ProgressBarUpdateThread);
thr.Start(this);
while (true)
{
/*lots of work loop here*/
}
}
//Thread To Run To Update The Progress Bar
public void ProgressBarUpdateThread(object param)
{
Form1 frm = param as Form1;
while (frm.runProgressBarUpdateThread)
{
//increase progress bar
frm.IncProgressBar(1);
//sleep for half a sec
System.Threading.Thread.Sleep(500);
}
}
public delegate void IncProgressBarDelegate(int value);
public void IncProgressBar(int value)
{
//need to invoke?
if (progressBar1.InvokeRequired)
{
progressBar1.Invoke(new IncProgressBarDelegate(IncProgressBar), value); //<-- seems to get stuck here
}
else
{
//update the progress bar value by value or reset to 0 when maximum is reached
progressBar1.Value = (progressBar1.Value + value >= progressBar1.Maximum) ? progressBar1.Minimum : progressBar1.Value + value;
progressBar1.Invalidate();
progressBar1.Update();
progressBar1.Refresh();
}
}
}