Post

Using a wait cursor doesn’t have to be hard…

Here is a simple disposable wait cursor class to simplify your mouse display needs…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
    /// <summary>
    /// The WaitCursor class. Implements the <see cref="IDisposable" />
    /// </summary>
    /// <seealso cref="IDisposable" />
    public class WaitCursor : IDisposable
    {
        /// <summary>
        /// The previous cursor.
        /// </summary>
        private readonly Cursor previousCursor;

        /// <summary>
        /// A value indicating whether the class has been disposed.
        /// </summary>
        private bool disposedValue;

        /// <summary>
        /// Initializes a new instance of the <see cref="WaitCursor" /> class.
        /// </summary>
        public WaitCursor()
        {
            this.previousCursor = Cursor.Current;
            Cursor.Current = Cursors.WaitCursor;
        }

        /// <summary>
        /// Finalizes an instance of the <see cref="WaitCursor" /> class.
        /// </summary>
        ~WaitCursor()
        {
            this.Dispose(disposing: false);
        }

        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting
        /// unmanaged resources.
        /// </summary>
        public void Dispose()
        {
            this.Dispose(disposing: true);
            GC.SuppressFinalize(this);
        }

        /// <summary>
        /// Releases unmanaged and - optionally - managed resources.
        /// </summary>
        /// <param name="disposing">
        /// <c>true</c> to release both managed and unmanaged resources; <c>false</c> to release
        /// only unmanaged resources.
        /// </param>
        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposedValue)
            {
                if (disposing)
                {
                }

                Cursor.Current = this.previousCursor;

                this.disposedValue = true;
            }
        }
    }
This post is licensed under CC BY 4.0 by the author.