The C# counterpart of MFC’s CWaitCursor
Hi,
For those who are not familiar with MFC, the CWaitCursor class provides a way to show a wait cursor, which is usually displayed as an hourglass, while you’re doing a lengthy operation. There are plenty of ways to modify the pointing device’s cursor under C#, however, the one I’m going to bring up here is simple and straight forward that has been used over the past years for my programs.
using System; using System.Threading; using System.Windows.Forms; /// <summary> /// Provides a utility class to handle the pointing device's wait cursor. /// </summary> public class WaitCursor : IDisposable { private static int refCount = 0; /// <summary> /// Initializes a new instance of the WaitCursor class. /// </summary> public WaitCursor() { Interlocked.Increment(ref refCount); Cursor.Current = Cursors.WaitCursor; } /// <summary> /// Sets the default cursor. /// </summary> public void Dispose() { if (Interlocked.Decrement(ref refCount) == 0) Cursor.Current = Cursors.Default; } }
Finally, to use the WaitCursor class, you just do the following:
using (new WaitCursor()) { //Do a lengthy operation }
That’s all for now, folks!
Sponsors
Circle up with me on Google+
Recent Comments