0

I have a Label and PictureBox element that in designer i set visibility as false.

now i try this :

   private void openExcelButton_Click(object sender, EventArgs e)
    {

        openExcelDialog.Filter = "Excel files|*.xls;*.xlsx;*.csv";
        DialogResult result = openExcelDialog.ShowDialog();
        if (result == DialogResult.OK) // Test result.
        {
            LoadingGIF.Visible = true;
            LoadingLabel.Text = "Loading...";
            LoadingLabel.Visible = true;
            string file = openExcelDialog.FileName;

            //more code
            LoadingGIF.Visible = false;
            LoadingLabel.Text = "Uploading Finished!";



        }
    }

Now when pressing the button and choosing a file nothing happens untill i finish the code in the //more code section and then the label changes.

Why does this happen?

eric.itzhak
  • 15,752
  • 26
  • 89
  • 142
  • 1
    Because your form won't receive more Windows' messages until it completes to process the 'click' one (then controls won't be painted as visible). Add (I don't like it) Application.DoEvents() before "more code" or make it asynchronous – Adriano Repetti Nov 25 '12 at 13:47

1 Answers1

1

The reason this happens is because your main thread is becoming non-responsive and not allowing the changes to happen in a sequential order. I had a very similar issue on a project a year ago. The suggested solution by MS is to use a background worker to open the file and manipulate it so the primary thread does not become non-responsive. Microsoft has a fairly decent example of how to use a background worker here: http://msdn.microsoft.com/en-us/library/cc221403(v=vs.95).aspx

Jammin411
  • 418
  • 3
  • 5