Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I display wait cursor during a WPF application's startup?

Here are the basic events I want to happen when my WPF application starts. This is very similar to how Word starts on my machine.

  1. Display busy cursor.
  2. Perform basic initialization. This takes a couple of seconds and needs to be done before splash screen is displayed.
  3. Display splash screen. This splash screen displays progress into more in-depth initialization and can take awhile (caches information from database).
  4. Display default cursor. Since splash screen is displaying progress now, there's no need to display a busy cursor.
  5. Once splash screen progress is complete, display main window.
  6. Close splash screen.

Everything works fine except for the displaying of the busy cursor prior to the splash screen being displayed. When I execute the application through a shortcut, the wait cursor flashes, but soon goes back to the default. I've tried different ways to set the Cursor but none work, but I think the problem is that I'm not in a control/window--I'm doing it from within App.xaml.cs. And, the properties I'm setting seem to be Windows Forms properties. Here is an excerpt from my code in App.xaml.cs.

protected override void OnStartup(StartupEventArgs e) {   base.OnStartup(e);    System.Windows.Forms.Application.UseWaitCursor = true;   //System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;   //System.Windows.Forms.Application.DoEvents();    Initialize();    SplashWindow splash = new SplashWindow();   splash.Show();    System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;    // Right now I'm showing main window right after splash screen but I will eventually wait until splash screen closes.   MainWindow main = new MainWindow();   main.Show(); } 
like image 878
bsh152s Avatar asked Jun 13 '12 18:06

bsh152s


People also ask

How do I change my cursor to hourglass in C#?

Hourglass Cursor [C#] If you want to change the mouse cursor at application level use static property Current of Cursor class. To show hourglass cursor assign value Cursors. WaitCursor. To return back the default behavior (to display control specific cursor) assign back value Cursors.

What is use wait cursor C#?

UseWaitCursor = true; This shows the wait cursor for the specified Form or Control and all its child controls until you set the UseWaitCursor property to false. If your operation blocks input not just in one window but for your entire application, you can set the Application.


2 Answers

This should work

Mouse.OverrideCursor = System.Windows.Input.Cursors.Wait; 

Use System.Windows.Input not System.Windows.Forms.

like image 187
Kevin DiTraglia Avatar answered Sep 27 '22 21:09

Kevin DiTraglia


If you have a task that takes a significant amount of time, and it is running on a non-GUI thread, (which is a good idea) you can use this code to change the application cursor:

Application.Current.Dispatcher.Invoke(() => {     Mouse.OverrideCursor = Cursors.Wait; }); 

When the busy process completes, use this:

Application.Current.Dispatcher.Invoke(() => {     Mouse.OverrideCursor = null; }); 
like image 25
user2069105 Avatar answered Sep 27 '22 23:09

user2069105