user3471683
user3471683

Reputation: 11

Loading gif image visible while the execution process runs

I am using window app and C#.. i have a picture which is invisible at the start of the app.. when some button is clicked, the picture box has to be shown..

i use this coding but the picture box is not visible

 private void btnsearch_Click(object sender, EventArgs e)
        {
            if (cmbproject.Text == "---Select---")
            {
                MessageBox.Show("Please Select Project Name");
                return;             
            }
            else
            {
                pictureBox1.Visible = true;
                pictureBox1.BringToFront();
                pictureBox1.Show();

                FillReport();
                Thread.Sleep(5000);
                pictureBox1.Visible = false;
        }

    }

Upvotes: 0

Views: 822

Answers (1)

Blorgbeard
Blorgbeard

Reputation: 103467

Don't use Sleep - that blocks the thread, which means no windows messages get processed, and your form won't get repainted.

Instead, you could use a Timer to hide the image after 5 seconds.

Add a timer to your form, and change your code to be something like this:

pictureBox1.Visible = true;    
FillReport();
timer1.Interval = 5000;
timer1.Start();

And in the timer event:

private void Timer1_Tick(object sender, EventArgs e) {
    pictureBox1.Visible = false;
    timer1.Stop();
}

Now your image should be visible for 5 seconds.

However, the form will still not repaint while FillReport is executing. If you need the image to be visible at that point, I suggest using a BackgroundWorker to execute FillReport so that it doesn't block the UI thread. Then you can hide the image in the RunWorkerCompleted event.

Upvotes: 2

Related Questions