How do I set the hourglass?

  • Thread starter Thread starter BoomBoom
  • Start date Start date
B

BoomBoom

I can't seem to locate the property for setting/resetting the cursor
hourglass. Can someone tell me what it is?
 
You want something like:

Cursor current = this.Cursor;
this.Cursor = Cursors.WaitCursor;

// Long operation
System.Threading.Thread.Sleep( 5000 );

this.Cursor = current;

HTH

RS
 
BoomBoom said:
I can't seem to locate the property for setting/resetting the cursor
hourglass. Can someone tell me what it is?

I have created a class that do it. It is easy to use, as you just have to
type
HG hg = new HG();

The class use the Constructor to set the cursor, and its Destructor to set
the cursor back to default. This way, you don't have to worry about 'hanging
apps'...

Create a new class and copy & paste:


using System;
using System.Windows.Forms;

namespace Whatever
{
public class HG
{
private Cursor curr=null;
public HG()
{
curr = Cursor.Current;
Cursor.Current = Cursors.WaitCursor;
}
~HG()
{
Cursor.Current = curr;
}
}
}

Per Rollvang
 
Per Rollvang said:
I have created a class that do it. It is easy to use, as you just have to
type
HG hg = new HG();

The class use the Constructor to set the cursor, and its Destructor to set
the cursor back to default. This way, you don't have to worry about 'hanging
apps'...

Create a new class and copy & paste:


using System;
using System.Windows.Forms;

namespace Whatever
{
public class HG
{
private Cursor curr=null;
public HG()
{
curr = Cursor.Current;
Cursor.Current = Cursors.WaitCursor;
}
~HG()
{
Cursor.Current = curr;
}
}
}


I wouldn't do this way. In C#, there is no guarantee that the destructor
will ever be called. It really depends on when the Garbage Collector calls
it, which is non-deterministic. My recommendation is that you just put the
cursor logic around your code:

curr = Cursor.Current;
Cursor.Current = Cursors.WaitCursor;

// run your code here.

Cursor.Current = curr;

Or if you wanted to guarantee that your cursor was reset, you could use
try/finally.

try
{
curr = Cursor.Current;
Cursor.Current = Cursors.WaitCursor;

// run your code here.
}
finally
{
Cursor.Current = curr;
}


Joe
 
Joe Mayo said:
I wouldn't do this way. In C#, there is no guarantee that the destructor
will ever be called. It really depends on when the Garbage Collector calls
it, which is non-deterministic. My recommendation is that you just put the
cursor logic around your code:

curr = Cursor.Current;
Cursor.Current = Cursors.WaitCursor;

// run your code here.

Cursor.Current = curr;

Or if you wanted to guarantee that your cursor was reset, you could use
try/finally.

try
{
curr = Cursor.Current;
Cursor.Current = Cursors.WaitCursor;

// run your code here.
}
finally
{
Cursor.Current = curr;
}


Joe

Hmm. I'm sorry to hear that. I use this class A LOT. ; )

I must update my GC knowledge...

Thanks for the info!

-Per

But, it DO work, and
 
Hi Per,

As a another solution (Joe's comment) you might modify your class to
implement IDisposable instead of finalizer:
namespace Whatever
{
public class HG: IDisposable
{
private Cursor curr=null;
public HG()
{
curr = Cursor.Current;
Cursor.Current = Cursors.WaitCursor;
}
public Dispose()
{
Cursor.Current = curr;
}
}
}

You might use something like:
<some code>
using (new HG())
{
<code>
}

I see the solution as an elegant one and use often.
 
Per said:
I have created a class that do it. It is easy to use, as you just have to
type
HG hg = new HG();

The class use the Constructor to set the cursor, and its Destructor to set
the cursor back to default. This way, you don't have to worry about 'hanging
apps'...

Create a new class and copy & paste:


using System;
using System.Windows.Forms;

namespace Whatever
{
public class HG
{
private Cursor curr=null;
public HG()
{
curr = Cursor.Current;
Cursor.Current = Cursors.WaitCursor;
}
~HG()
{
Cursor.Current = curr;
}
}
}

Per Rollvang
 
Is there a "twrilling" version of the hourglass?

Miha Markic said:
Hi Per,

As a another solution (Joe's comment) you might modify your class to
implement IDisposable instead of finalizer:
namespace Whatever
{
public class HG: IDisposable
{
private Cursor curr=null;
public HG()
{
curr = Cursor.Current;
Cursor.Current = Cursors.WaitCursor;
}
public Dispose()
{
Cursor.Current = curr;
}
}
}

You might use something like:
<some code>
using (new HG())
{
<code>
}

I see the solution as an elegant one and use often.

--
Miha Markic [MVP C#] - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com

Per Rollvang said:
I have created a class that do it. It is easy to use, as you just have to
type
HG hg = new HG();

The class use the Constructor to set the cursor, and its Destructor to set
the cursor back to default. This way, you don't have to worry about 'hanging
apps'...

Create a new class and copy & paste:


using System;
using System.Windows.Forms;

namespace Whatever
{
public class HG
{
private Cursor curr=null;
public HG()
{
curr = Cursor.Current;
Cursor.Current = Cursors.WaitCursor;
}
~HG()
{
Cursor.Current = curr;
}
}
}

Per Rollvang
 
Thanks Miha!

I did not know using is a statement as well as a directive...Smart indeed.
Now I guess I'll never again suffer from 'hanging app' due to my sloppy
mouse-routines...

-Per

Miha Markic said:
Hi Per,

As a another solution (Joe's comment) you might modify your class to
implement IDisposable instead of finalizer:
namespace Whatever
{
public class HG: IDisposable
{
private Cursor curr=null;
public HG()
{
curr = Cursor.Current;
Cursor.Current = Cursors.WaitCursor;
}
public Dispose()
{
Cursor.Current = curr;
}
}
}

You might use something like:
<some code>
using (new HG())
{
<code>
}

I see the solution as an elegant one and use often.

--
Miha Markic [MVP C#] - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com

Per Rollvang said:
I have created a class that do it. It is easy to use, as you just have to
type
HG hg = new HG();

The class use the Constructor to set the cursor, and its Destructor to set
the cursor back to default. This way, you don't have to worry about 'hanging
apps'...

Create a new class and copy & paste:


using System;
using System.Windows.Forms;

namespace Whatever
{
public class HG
{
private Cursor curr=null;
public HG()
{
curr = Cursor.Current;
Cursor.Current = Cursors.WaitCursor;
}
~HG()
{
Cursor.Current = curr;
}
}
}

Per Rollvang
 
If you really want to destruct an object, use the using block
using (HG cr=new HG())
{

}

The HG object will automatically be disposed at the end of the using
block.

with regards,


J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com
 
If you really want to destruct an object, use the using block
using (HG cr=new HG())
{

}

The Cursor object will automatically be disposed at the end of the using
block.

with regards,


J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com
 
Back
Top