English 中文(简体)
How to display animated gif during long asynchronous operation?
原标题:

I have a Winforms app in C# that calls calls a method asynchronously and uses a callback. I would like to display an animated gif to let the end user know that work is being done. I would like to have the animated gif hover over the center of the form.

How can I do this?

Update: Thanks. I guess the step I was missing was to use a Picture Box to hold the gif. The following seems to be doing the trick of showing the gif and like jmatthews3865 said below I can just set the visible property of the PictureBox to false to hide it.

private ShowAnimatedGif()
{
 PictureBox pb = new PictureBox();
 this.Controls.Add(pb);
 pb.Left = (this.Width / 2) - (pb.Width / 2);
 pb.Top = (this.Height / 2) - (pb.Height / 2);
 pb.Image = Resources.AnimatedGifHere;
 pb.Visible = true;
}
最佳回答

in your form, simply include the image with it s visible property set to false.

from the event which calls the long running async process (button1_click etc.), set the images visibility property to true. event fires, image appears, async process runs and your ui thread should still be responsive.

in your callback event set the images visible property to false to indicate that the process is complete.

问题回答

Need some code to give an exact answer, but this is fairly trivial, insert the gif before you make the asynchronous call, then remove it in the callback.

This is the answer. I m using LoadingCircle which is an animated gif component.

public partial class Form1 : Form
{
   public delegate void ProcessAnimation(bool show);
   ProcessAnimation pa;

   public Form1()
    {
        InitializeComponent();

        pa = this.ShowAnimation;
    }    

private void button2_Click(object sender, EventArgs e)
    {

        Thread tr = new Thread(FlushToServer);
        tr.Start();

    }

    private void ShowAnimation(bool show)
    {
        if (show)
        {
            loadingCircle1.Visible = true;
            loadingCircle2.Active = true;

        }
        else
        {
            loadingCircle1.Visible = false;
            loadingCircle1.Active = false;

        }
    }




    private void FlushToServer()
    {

        this.Invoke(this.pa,true);
        //your long running process
        System.Threading.Thread.Sleep(5000);
        this.Invoke(this.pa,false); 
    } 
   }

i modify the above code a bit and it will not throw error "c# invoke or begininvoke cannot be called on a control until the window handle has been created."

namespace AnimateUI
{
    public partial class Form1 : Form
    {
        public delegate void ProcessAnimation(bool show);
        ProcessAnimation pa;


        public Form1()
        {
            InitializeComponent();
            pa = this.ShowAnimation;
            pictureBox1.Visible = false;
        }

        private void ShowAnimation(bool show)
        {
            if (show)
            {
                pictureBox1.Visible = true;
            }
            else
            {
                pictureBox1.Visible = false;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Thread tr = new Thread(StartTask);
            tr.Start();
        }

        private void StartTask()
        {
            if (!this.IsHandleCreated)
                this.CreateControl();

            this.Invoke(this.pa, true);
            System.Threading.Thread.Sleep(15000);
            this.Invoke(this.pa, false);
        }

    }

}




相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签