Windows Develop Bookmark and Share   
 index > Windows Forms General > Display animated gif image while the background processing happens
 

Display animated gif image while the background processing happens

Hi All,
I am developing a windows app where there are scenario's which take a processing time of upto 3-4 seconds.

What I am trying to do here is start a new thread and display an animated image just to indicate that the processing is going on in the background. At the same time, I even want to disable the main form.

Can anyone suggest how to go ahead with this. I am looking for a simple method that can be called during this processing. I am trying to use a picture library in this case.


Thanks
Thanks Sridhar
  • Changed TypeSridharV Monday, September 14, 2009 6:17 AM
  •  
SridharV  Monday, September 14, 2009 5:55 AM

Hi SridharV,

To show an animated gif in the background of a form, we can set the BackgroundImage of the Form to a gif image and then use ImageAnimator to handle the animation. This is a code snippet:

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            //Enable double buffering to reduce flicker.
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            //Enable paint via message to reduce flicker. 
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
        }
        //Whether the animation starts.
        bool currentlyAnimating = false;
        //Indicate if update the animation.
        bool isAnimating = true;
        protected override void OnPaintBackground(PaintEventArgs e)
        {
            if (isAnimating)
            {
                //Begin the animation.
                AnimateImage();
                //Update the frames. The cell would paint the next frame of the image late on.
                ImageAnimator.UpdateFrames();
            }
            base.OnPaintBackground(e);
        }
        //This method begins the animation.
        public void AnimateImage()
        {
            if (!currentlyAnimating)
            {
                ImageAnimator.Animate(this.BackgroundImage, new EventHandler(this.OnFrameChanged));
                currentlyAnimating = true;
            }
        }
        private void OnFrameChanged(object o, EventArgs e)
        {
            //Force to redraw the form.
            this.Invalidate();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("alala");
        }
    }

This is a similar thread: http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/0d9e790e-6816-40e7-96fe-bbf333a4abc0/.

To disable a form, we can set the Enable property of the Form to false.

Let me know if this helps or not.
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Wednesday, September 16, 2009 5:38 AM

Hi SridharV,

To show an animated gif in the background of a form, we can set the BackgroundImage of the Form to a gif image and then use ImageAnimator to handle the animation. This is a code snippet:

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            //Enable double buffering to reduce flicker.
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            //Enable paint via message to reduce flicker. 
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
        }
        //Whether the animation starts.
        bool currentlyAnimating = false;
        //Indicate if update the animation.
        bool isAnimating = true;
        protected override void OnPaintBackground(PaintEventArgs e)
        {
            if (isAnimating)
            {
                //Begin the animation.
                AnimateImage();
                //Update the frames. The cell would paint the next frame of the image late on.
                ImageAnimator.UpdateFrames();
            }
            base.OnPaintBackground(e);
        }
        //This method begins the animation.
        public void AnimateImage()
        {
            if (!currentlyAnimating)
            {
                ImageAnimator.Animate(this.BackgroundImage, new EventHandler(this.OnFrameChanged));
                currentlyAnimating = true;
            }
        }
        private void OnFrameChanged(object o, EventArgs e)
        {
            //Force to redraw the form.
            this.Invalidate();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("alala");
        }
    }

This is a similar thread: http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/0d9e790e-6816-40e7-96fe-bbf333a4abc0/.

To disable a form, we can set the Enable property of the Form to false.

Let me know if this helps or not.
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Wednesday, September 16, 2009 5:38 AM

You can use google to search for other answers

Custom Search

More Threads

• Custom DataGridViewColumn
• Updating DataGridView from Background Worker Thread.
• How to: Make EventHandler work under multithread programs?
• Ocx component in a Windows Service
• How to make picture box visible from another thread
• question about color of tab
• How do I get event passed up past FlowLayoutPanel?
• Drawing multiple circles
• What do I use instead of the OnCurrent event?
• Always on top window...