I have a Windows Form, which is a modal mdi child, that is suppose to be shown when some intense background work is going on, so the user cannot use any of the controls until that work is finished.
It is very simple, here is the code.
public partial class ProgressForm : Form
{
private int periodCount = 5;
public ProgressForm(String message)
{
InitializeComponent();
messageLabel.Text = message;
}
public void startThread()
{
Thread t = new Thread(new ThreadStart(doWork));
t.IsBackground = true;
t.Start();
}
void doWork()
{
while (true)
{
if (periodCount == 5)
{
periodCount = 1;
}
else
{
periodCount++;
}
switch (periodCount)
{
case 1: periodsLabel.Text = "."; break;
case 2: periodsLabel.Text = ". ."; break;
case 3: periodsLabel.Text = ". . ."; break;
case 4: periodsLabel.Text = ". . . ."; break;
case 5: periodsLabel.Text = ". . . . ."; break;
}
}
}
}
but, the periodsLabel.Text does not change as it is suppose to! How do I get it to update the UI while doing something else in the background?
ProgressForm progressForm = new ProgressForm("Your database data is being exported, please wait.");
progressForm.ShowDialog();
progressForm.startThread();