papadi Development Blog

.NET and Software Development by Dimitris Papadimitriou 
Filed under

Windows Forms

 

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).

Filed under  //   .NET   C#   VB   Windows Forms  

Comments [0]

Most of things require much lesser code in .NET. Some things NOT!!!

Intro | After a few years dealing with .NET I can say that I'm very please with the fact that I have been working with Microsoft Development tools in my carreer. And this includes starting from GW-Basic, moving to VB5 and 6 and now 7 and 8 and a little C#.
Less Code | Most of things require much lesser code to implement. The code is almost completely managed and there are much fewer chances of using third part components or WIN32 API to get thigs done.
However | There are some other things however that require more code than before. And the question is why? For instance, to detect if the user clicked on the X button of a window to close it was 5 lines of code, including the event declaration, in VB6:

[code language="vb"]
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    If UnloadMode = vbFormControlMenu Then
        Cancel = True
    End If
End Sub
[/code]

Now, the code in NET Framework 2 is the following, as David Kean indicated to me in this thread http://forums.microsoft.com/msdn/ShowPost.aspx?PostID=63488 (its in C#, but the same applies to VB8). It about 10 lines of code, without the brackets, which include a class level variable declaration:

[code language="c#"]
using System;
using System.ComponentModel;
using System.Security.Permissions;
using System.Windows.Forms;

namespace MyApplication
{
    public class Form1 : Form
    {
        private const int WM_SYSCOMMAND = 0x0112;
        private const int SC_CLOSE = 0xF060;
        private bool _UserClose;

        public Form1()
        {
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            if (_UserClose)
            {
                MessageBox.Show("The user clicked the X button.");
            }
            base.OnClosing(e);
        }

        [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
        protected override void WndProc(ref Message m)
        {
            if (m.Msg == WM_SYSCOMMAND && ((int)m.WParam & 65520) == SC_CLOSE)
            {
                _UserClose = true;
            }
            base.WndProc(ref m);
            _UserClose = false;
        }
    }
}
[/code]

Filed under  //   .NET   C#   Desktop   VB   Visual Basic 6   Windows Forms  

Comments [0]