I am working on a small project where I open a windows form from within another process to show a progress bar.
So my form code looks like this:
public partial class ProgressForm : Form
{
int currentValue = 0;
int pbMax;
bool cancelled = false;
public ProgressForm(int pbMax)
{
InitializeComponent();
this.pbMax = pbMax;
this.progressBar1.Maximum = pbMax;
}
public void updateProgressBar(int newValue)
{
currentValue = newValue;
this.progressBar1.Value = newValue;
}
public bool getCancelledStatus()
{
return cancelled;
}
private void button1_Click(object sender, EventArgs e)
{
this.cancelled = true;
}
}
where button1 is my "cancel" button.
My plan was to enable a user to cancel the progress of another task by clicking the Cancel button.
The code in my other task is:
ProgressForm pf = new ProgressForm(MaxValue);
pf.Show();
bool cancelled = false;
for (int i = 0; i<pbMax; i++)
{
if (cancelled == true)
break;
pf.updateProgressBar(i + 1);
/***** DO WORK HERE ******/
cancelled = pf.getCancelledStatus();
}
but I can't even click the cancel button. The whole form freezes up when showing the progress bar. Is there anything I can do about this? Can I use threading or something like that?