开发者

Threading with Modal Progress Form, can't get the label's text property to update

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();


First, in my humble opinion, you should not just throw away a thread like you do.

The best practice is using a BackgroundWorker Thread.

Second, you form isn't modal at all, as you only show it using the Show() method. In order to make it a modal form, you need to make it a dialog using the ShowDialog() method.

As to why exactly your Form is crashing is quite out of scope from now on. Please consider following the following steps:

  1. Instantiate a BackgroundWorker class;
  2. Make the BackgroundWorker.DoWork() method do the dirty work for you;
  3. Make sure you set the BackgroundWorker.WorkerReportsProgress = true in the component model property window in design;
  4. Make sure you ReportProgress() using the ReportProgress(int) method.

Please see this question (C#: Populating a UI using separate threads.) and my code sample which simply explains, I think, the use of a BackgroundWorker class instance.

Note: Still looking for another example.

EDIT #1

Here's a good article on threads: Threading in C#.
'Cause Jon Skeet said so! Multi-threading in .NET: Introduction and suggestions.


It's likely that your app is crashing because you are trying to access your form elements' properties directly from a thread (e.g. when you call PeriodsLabel.Text) rather than using BeginInvoke from your thread to call a method on your form which will do the property accessing.

But it's better/simpler to use the BackgroundWorker class for these kinds of things.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜