We all know that the using
statement is really good for resources you want to cleanup in a timely manner, such as an open file or database connection.
I was wondering if it would be considered a good thing to use the statement in cases where resources cleanup isn't the goal of the Dispose()
method, but rather resetting to a previous state.
For example, a class that allows a using statement to wrap a procedure that takes a noticeable amount of time and changes the Cursor to a waiting state.
class CursorHelper : IDisposable
{
readonly Cursor _previousState;
public CursorHelper(Cursor newState)
{
_previousState = Cursor.Current;
Cursor.Current = newState;
}
public void Dispose()
{
Cursor.Current = _previousState;
}
}
Then the class can be used as such, without worrying about reverting the Cursor when your are done.
public void TimeIntensiveMethod()
{
using (CursorHelper ch = new CursorHelper(Cursors.WaitCursor))
{
// something that takes a long time to complete
}
}
Is this an appropriate use of the using
statement?
There are certainly precedents for (ab)using the using
statement in this way, for example FormExtensions.BeginForm in the ASP.NET MVC framework. This renders a <form>
closing tag when it's disposed, and it's main purpose is to enable a more terse syntax in MVC views. The Dispose
method attempts to render the closing tag even if an exception is thrown, which is slightly odd: if an exception is thrown while rendering a form, you probably don't want to attempt to render the end tag.
Another example is the (now deprecated) NDC.Push method in the log4net framework, which returns an IDisposable
whose purpose is to pop the context.
Some purists would say it's an abuse, I suggest you form your own judgement on a case-by-case basis. Personally I don't see anything wrong with your example for rendering an hourglass cursor.
The discussion linked in a comment by @I4V has some interesting opinions - including arguments against this type of "abuse" from the ubiquitous Jon Skeet.