Chris Klepeis
Chris Klepeis

Reputation: 9993

.NET Windows Forms Refresh Control

I have a simple program which loops through a resultset when a button is pressed and performs actions against this resultset (stored in a DataTable). During the loop I update the text of a RichTextBox, 2 labels, and a ToolStripStatusLabel. These are not refreshing during the loop so after each item is processed in the loop I do a this.Refresh(). This works unless I click on anything in the program, at which time it says it's not responding, and nothing is refreshed in the form until the foreach loop completes.

private void myBtn_Click(object sender, EventArgs e)
{
    // Query database and store results in DataTable dt.

    foreach (DataRow dr in dt.Rows)
    {
       // Process row data.
       // Update RichTextBox, labels, and ToolStripStatusLabel.
       this.Refresh();
    }
}

Is something wrong here? Is there a better way to do this? This is a Windows Forms application using C#.

Upvotes: 1

Views: 5503

Answers (1)

user27414
user27414

Reputation:

It says "not responding" because you're tying up the GUI thread. If you have a lengthy operating to perform, consider using a background thread instead.

Check out BackgroundWorker to get started.

And here's a threading tutorial, just in case.

Upvotes: 5

Related Questions