This topic is also covered by the RAII topic as described in
RAII (Resource Acquisition Is Initialization) C# Helper Classes[
^].
Applying the RAII pattern results in more expressive code:
using (new RAIIGuard<Cursor>(()=>owner.Cursor, c=>owner.Cursor = c))
{
owner.Cursor = Cursor.WaitCursor;
}
If you have this situation more often, then you want to create your own class as mentioned in your code, e.g.:
public class CursorLifeTime: RAIIGuard<Cusror>
{
CursorLifeTime(Control owner) : base(()=>owner.Cursor, c=>owner.Cursor=c) {}
}
Usage:
using (new CursorLifeTime(owner))
{
owner.Cursor = Cursor.WaitCursor;
}
<Begin-Edit>
(edited to address issues rised in the comments below):
There is a subtle difference to your code: as shown here above, I would recommend to build your code on existing building bocks (e.g. RAII pattern) and only assign the tasks of
storing and
restoring to the RAII class/code.
Setting the Wait cursor is part of the normal function, not part of the RAII.
The sample RAII code is:
public class RAIIGuard<T>: IDisposable
{
private Action<T> Cleanup { get; set; }
public T Item { get; private set; }
public RAIIGuard(Func<T> init, Action<T> cleanup)
{
Cleanup = cleanup;
Item = (init != null) ? init() : default(T);
}
public void Dispose() { if (Cleanup != null) Cleanup(Item); }
}
And the
WaitCursorLifeTime
class variant that also sets the cursor is:
public class WaitCursorLifeTime: RAIIGuard<Cusror>
{
WaitCursorLifeTime(Control owner) : base(()=>owner.Cursor, c=>owner.Cursor=c)
{ owner.Cursor = Cursors.WaitCursor; }
}
Usage:
using (new WaitCursorLifeTime(owner))
{
}
<End-Edit>
I usually create, based on that
CursorLifeTime
class a class that triggers a timer and only after a certain time, turns that Wait Cursor on (and off if the block is left). The users' perception is more positive, if the cursor does not flicker too much with each action that lasts a very tiny moment - only the actions that last more than say 500 ms should turn on the Wait cursor.
Cheers
Andi