Home
Manage Your Code
Snippet: C# BusyIndicator (C#)
Title: C# BusyIndicator Language: C#
Description: Shows a wait cursor during a long task. Views: 917
Author: Diego Guidi Date Added: 7/18/2007
Copy Code  
1namespace Helpers
2{
3    public static class BusyIndicator
4    {
5        private static readonly AsyncCallback   nullCallback    = delegate { };
6        private static readonly object[]        emptyArray      = new object[] { };
7
8        public static void ShowWhile(Control control, ThreadStart method)
9        {
10            if (method == null)
11                throw new ArgumentException("Missing method definition", "method");
12
13            if (control == null)
14            {
15                method.BeginInvoke(nullCallback, emptyArray);
16                return;
17            }
18
19            control.Cursor = Cursors.WaitCursor;
20            MethodInvoker callback = delegate   { control.Cursor = Cursors.Default; };
21            AsyncCallback invoke   = delegate   { control.Invoke(callback); };
22            method.BeginInvoke(invoke, emptyArray);           
23        }
24    }
25}
Usage
BusyIndicator.ShowWhile(this, delegate
{
    // Do stuff
}