Setting and restoring cursor for a time consuming operation in a single line of code
For the proper handling of the Hourglass during a time consuming operation the following code is required (more or less):
| C# | VB.NET |
| this.UseWaitCursor = true; try { // DO JOB this.UseWaitCursor = false; } catch (Exception ex) { this.UseWaitCursor = false; throw ex; } |
Me.UseWaitCursor = True Try ' DO JOB Me.UseWaitCursor = False Catch ex As Exception Me.UseWaitCursor = False Throw ex End Try |
What if we wanted to be more accurate and restore the cursor to it's original state, which could be 'true' and not 'false'? Then we should declare a local variable, hold the original state and restore it later.
How would you like to use the following code!? Isn't it much more elegant?
| C# | VB.NET |
| using (new WaitCursorHandler(this)) { // DO JOB } |
Using WaitCursorHandler(Me) ' DO JOB End Using |
Simply add one of these files into your project (code requires .NET 2.0).
