Displaying wait cursor in while backgroundworker is running

67,740

Solution 1

Does UseWaitCursor work? (Set to true when calling RunWorkerAsync(), and false when the completion event is called). What are you using to set the cursor now?

Solution 2

Don't display a wait cursor for this - instead, use a control on your form to indicate that the backgroundworker is busy doing something. The wait cursor is an appropriate indicator for the UI thread to use (since it indicates that the user can't/shouldn't touch anything), but it's not appropriate for something happening in the background.

Solution 3

In WPF, I've done this by setting the Mouse.OverrideCursor property to Cursors.Wait before I start the Backgroundworker, and then resetting it to null in the RunWorkerCompleted event. Seems to work pretty well so far.

public void go()
{
    BackgroundWorker thread = new BackgroundWorker();

    thread.DoWork += run;
    thread.RunWorkerCompleted += taskCompleted;
    thread.WorkerReportsProgress = true;

    // Change mouse cursor to busy
    Mouse.OverrideCursor = Cursors.Wait;

    thread.RunWorkerAsync();
}

private void taskCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    // Reset mouse cursor
    Mouse.OverrideCursor = null;
}

Solution 4

You should use the BGW's RunWorkerCompleted event to set your cursor back to the default.

EDIT:

Set your cursor to wait by calling this code before starting up your BGW:

this.Cursor = Cursors.WaitCursor;

Reset your cursor in the BGW's RunWorkerCompleted event by:

this.Cursor = Cursors.Default;

Solution 5

There are a lot of answers here already but I found another solution that worked for me so I'd figured I list it.

private void ShowWaitCursor(){
    Application.UseWaitCursor = true; //keeps waitcursor even when the thread ends.
    System.Windows.Forms.Cursor.Current = Cursors.WaitCursor; //Normal mode of setting waitcursor
}

private void ShowNormalCursor(){
    Application.UseWaitCursor = false;
    System.Windows.Forms.Cursor.Current = Cursors.Default;
}

I use both in line like this and it seams to work the way I expect all the time. Especially when I want to have a wait cursor showing while a worker is running. Hope this helps anyone else still looking.

Share:
67,740
arc1880
Author by

arc1880

Will fill out later. Code Monkey.

Updated on December 24, 2021

Comments

  • arc1880
    arc1880 over 2 years

    During the start of my windows application, I have to make a call to a web service to retrieve some default data to load onto my application. During the load of the form, I run a backgroundworker to retrieve this data. I want to display the wait cursor until this data is retrieved. How would I do this?

    I've tried setting the wait cursor before calling the backgroundworker to run. When I report a progress of 100 then I set it back to the default cursor. The wait cursor comes up but when I move the mouse it disappears.

    Environment:

    • Windows 7 Pro 64-bit
    • VS2010 C# .NET 4.0
    • Windows Forms

    EDIT: I am setting the cursor the way Jay Riggs suggested. It only works if I don't move the mouse.

    **UPDATE: I have created a button click which does the following: When I do the button click and move my mouse, the wait cursor appears regardless if I move my mouse or not.

    void BtnClick()
    {
      Cursor = Cursors.WaitCursor;
      Thread.Sleep(8000);
      Cursor = Cursors.Default;
    }
    

    If I do the following: I see the wait cursor and when I move the mouse it disappears inside the form. If I move to my status bar or the menu bar the wait cursor appears.

    Cursor = Cursors.WaitCursor;
    if (!backgroundWorker.IsBusy)
    {
      backGroundWorker.RunWorkerAsync();
    }
    
    void backGroundWorkerDoWork(object sender, DoWorkEventArgs e)
    {
      Thread.Sleep(8000);
    }
    
    void backGroundWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
      Cursor = Cursors.Default;
    }
    

    If I do the following: The wait cursor appears and when I move the mouse it still appears but will sometimes flicker off and on when moving in text fields. Although the cursor changes to the wait cursor, it doesn't prevent you from clicking on anything.

    if (!backgroundWorker.IsBusy)
    {
      backGroundWorker.RunWorkerAsync();
    }
    
    void backGroundWorkerDoWork(object sender, DoWorkEventArgs e)
    {
      UseWaitCursor = true;
      Thread.Sleep(8000);
    }
    
    void backGroundWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
      UseWaitCursor = false;
    }
    
  • arc1880
    arc1880 about 13 years
    When I change the cursor to a wait cursor before I call the background worker and change the cursor back to default in the RunWorkerCompleted event it still disappears when I move the mouse.
  • arc1880
    arc1880 about 13 years
    What references do I include to access the Mouse.OverrideCursor?
  • WildCrustacean
    WildCrustacean about 13 years
    System.Windows.Input, I think. I also just realized that Mouse.OverrideCursor might just be for WPF, are you using WPF or Winforms?
  • arc1880
    arc1880 about 13 years
    The only thing I can include is System.Windows.Input.Manipulations and I don't see OverrideCursor anywhere. Is this only in WPF? I'm using windows forms.
  • WildCrustacean
    WildCrustacean about 13 years
    Yeah, I think Mouse.OverrideCursor is only in WPF. How are you doing it now? Setting Cursor.Current? If so you might search your code for places that you set Cursor.Current, maybe it is getting set in a mouse move event handler somewhere.
  • arc1880
    arc1880 about 13 years
    This does work. Although for some reason when you move the mouse on a text field it flickers on and off. But the wait cursor does stay there until the process is done.
  • arc1880
    arc1880 about 13 years
    Is there any reason why you should not use this method??
  • Mark Sowul
    Mark Sowul about 13 years
    Are you setting the property on the form or the textbox?
  • arc1880
    arc1880 about 13 years
    The form. I also noticed that doing this doesn't prevent you from clicking on anything.
  • Mark Sowul
    Mark Sowul about 13 years
    No, it doesn't. You'll want to set the form (or at least part of it; you probably don't want to prevent them from closing the form) Enabled property to false and then reset it to true in the completed event.
  • arc1880
    arc1880 about 13 years
    If I do set the Enabled property to false on the form, I don't see the wait cursor anymore.
  • David Sherret
    David Sherret over 11 years
    This works for me in WPF without even having a background worker.