JadedEric
JadedEric

Reputation: 2073

Animated GIF in please wait from

I have a please wait form in a windows application that gets rendered before a long running task, the please wait contains an animated gif that needs to "spin" during this process but I can't get this work.

I finally got the form to display correctly using the following code:

frmChooser chooser = frmChooser.Instance;
chooser.Url = this.UrlTextBox.Text;                
frmBuildNotification fb = new frmBuildNotification();
fb.Show(this);
fb.Update();
Application.DoEvents();
chooser.BuildTreeView();
fb.Close();
this.Hide();
chooser.Show(this);

The GIF is contained within a PictureBox control on frmBuildNotification

I've read somewhere that you'll need to repaint the form for the image to animate, but this seems a bit tedious?

Upvotes: 0

Views: 783

Answers (1)

Brad Rem
Brad Rem

Reputation: 6026

You need to keep the DoEvents message pump going and it also helps if you throw your notification form on another thread. Try this:

        bool done = false;
        frmBuildNotification fb = null;
        ThreadPool.QueueUserWorkItem((x) =>
        {
            using (fb = new frmBuildNotification())
            {
                fb.Show();
                while (!done)
                    Application.DoEvents();
                fb.Close();
            }
        });
        frmChooser chooser = frmChooser.Instance;
        chooser.Url = this.UrlTextBox.Text;
        chooser.BuildTreeView();
        done = true;
        this.Hide();
        chooser.Show(this); 

Upvotes: 1

Related Questions