Skip to content

Instantly share code, notes, and snippets.

@joe-oli
Last active February 1, 2024 09:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joe-oli/fc4e03773bb59b31679c737a60978023 to your computer and use it in GitHub Desktop.
Save joe-oli/fc4e03773bb59b31679c737a60978023 to your computer and use it in GitHub Desktop.
Hourglass in WPF
private void Button_Click(object sender, RoutedEventArgs e)
{
// Change the cursor to an hourglass
Mouse.OverrideCursor = Cursors.Wait;
try
{
// Perform the long-running operation here
// ...
}
finally
{
// Always ensure the cursor is reset even if an error occurs
Mouse.OverrideCursor = null;
}
}
If you want to show an hourglass cursor only over the specific control (like a button), you can use the `Cursor` property of the control:
```csharp
private void Button_Click(object sender, RoutedEventArgs e)
{
var button = (Button)sender;
// Change the cursor of the button to an hourglass
button.Cursor = Cursors.Wait;
try
{
// Perform the long-running operation here
// ...
}
finally
{
// Always ensure the cursor is reset even if an error occurs
button.Cursor = null;
}
}
```
This code changes the cursor to an hourglass only when the mouse is over the button¹.
Please note that if your long-running operation is running on the UI thread, the UI will become unresponsive and the hourglass cursor might not show up until the operation is complete. To avoid this, consider running the long-running operation on a separate thread or using async/await¹.
Source: Conversation with Bing, 01/02/2024
(1) c# - display Hourglass when application is busy - Stack Overflow. https://stackoverflow.com/questions/3480966/display-hourglass-when-application-is-busy.
(2) Simplest way to create busy animation in WPF - Stack Overflow. https://stackoverflow.com/questions/2934732/simplest-way-to-create-busy-animation-in-wpf.
(3) How to generate event handler automatically in WPF, C#?. https://stackoverflow.com/questions/59683990/how-to-generate-event-handler-automatically-in-wpf-c.
(4) How to programmatically click a button in WPF? - Stack Overflow. https://stackoverflow.com/questions/728432/how-to-programmatically-click-a-button-in-wpf.
(5) undefined. http://www.loadinfo.net/.
(6) undefined. http://www.codeproject.com/Articles/29545/FrameBasedAnimation-Animating-multiple-properties.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment