0

I have a Windows Forms application, in my application i load files into a list box and sometimes this could take few seconds so in this time i want to show "Spinning Wheel” and i found this Gif: http://www.ajaxload.info/ is it possible to add it to my application while my application is busy over the controllers ?

Flot2011
  • 4,601
  • 3
  • 44
  • 61
user3271698
  • 145
  • 3
  • 9
  • 19
  • 2
    Please don't use a GIF. Let the system do this for you. – David Heffernan Feb 24 '14 at 14:39
  • I cannot using Cursor.Current = Cursors.WaitCursor, only System.Windows.Forms.DataVisualization.Charting.Cursor – user3271698 Feb 24 '14 at 14:46
  • You can use a backgroundworker that updates a progressbar/image if you want to use that. – Tan Feb 24 '14 at 14:49
  • What do you mean updates a image ? – user3271698 Feb 24 '14 at 14:50
  • @user3271698 what I mean is when you are doing your work in the backgroundworkder you can show a dialog with an image of a progressbar then close the dialog when task is finished. Or you can use the default progressbar. – Tan Feb 24 '14 at 15:49
  • it is important that you use somekind of async call/Backgroundworker if not the applciation will just freeze and you will not see your progressbar. Take a look at this http://www.codeproject.com/Tips/83317/BackgroundWorker-and-ProgressBar-demo – Tan Feb 24 '14 at 15:53

2 Answers2

3

Yes

Found some old code from a project where I had it. Edited out a few things, you should be able to get it working easily.

Invoke it:

GuiCursor.WaitCursor(() => { yourclass.DoSomething(); });

The class

internal class GuiCursor
{

    private static GuiCursor instance = new GuiCursor();

    private GuiCursor() { }
    static GuiCursor() { }

    internal static void WaitCursor(MethodInvoker oper)
    {
        if (Form.ActiveForm != null && !Thread.CurrentThread.IsBackground)
        {
            Form myform = Form.ActiveForm;
            myform.Cursor = Cursors.WaitCursor;

            try
            {
                oper();
            }
            finally
            {
                myform.Cursor = Cursors.Default;
            }
        }
        else
        {
            oper();
        }
    }

    internal static void ToggleWaitCursor(Form form, bool wait)
    {
        if (form != null)
        {
            if (form.InvokeRequired)
            {
                form.Invoke(new MethodInvoker(() => { form.Cursor = wait? Cursors.WaitCursor : Cursors.Default; }));
            }
            else
            {
                form.Cursor = wait ? Cursors.WaitCursor : Cursors.Default;
            }
        }
    }

    internal static void Run(Form form)
    {
        try
        {
            Application.Run(form);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }
}

As by request, an example. Create a new winform project to test it out. As default you get a Form1. Add a button to it, double click on it so you get a autogenerated method to it.

Replace the class Form1 with this.

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            GuiCursor.WaitCursor(() => { DoSomething(); });
        }

        private void DoSomething()
        {
            Thread.Sleep(3000);
        }
    }



    internal class GuiCursor
    {

        private static GuiCursor instance = new GuiCursor();

        private GuiCursor() { }
        static GuiCursor() { }

        internal static void WaitCursor(MethodInvoker oper)
        {
            if (Form.ActiveForm != null && !Thread.CurrentThread.IsBackground)
            {
                Form myform = Form.ActiveForm;
                myform.Cursor = Cursors.WaitCursor;

                try
                {
                    oper();
                }
                finally
                {
                    myform.Cursor = Cursors.Default;
                }
            }
            else
            {
                oper();
            }
        }

        internal static void ToggleWaitCursor(Form form, bool wait)
        {
            if (form != null)
            {
                if (form.InvokeRequired)
                {
                    form.Invoke(new MethodInvoker(() => { form.Cursor = wait ? Cursors.WaitCursor : Cursors.Default; }));
                }
                else
                {
                    form.Cursor = wait ? Cursors.WaitCursor : Cursors.Default;
                }
            }
        }

        internal static void Run(Form form)
        {
            try
            {
                Application.Run(form);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }
Johan
  • 753
  • 2
  • 11
  • 31
2

A little trick to do this could be to use a PictureBox with image in it. On button click, make the PictureBox visible and hide it again after click operation is completed.

danish
  • 5,550
  • 2
  • 25
  • 28