Displaying wait cursor in while backgroundworker is running

后端 未结 9 1695
傲寒
傲寒 2020-12-30 03:50

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

相关标签:
9条回答
  • 2020-12-30 04:07

    Something else must be setting the cursor. The code below sets a wait cursor when the timer starts, and then sets the cursor back to the previous value after the timer expires. It works as expected: the wait cursor remains throughout the interval. Of course, moving the cursor outside the form will show the cursor for whatever window the mouse cursor is over, but moving it back to my form shows the wait cursor.

    public partial class MyDialog : Form
    {
        private Timer myTimer;
        public MyDialog()
        {
            InitializeComponent();
            myTimer = new Timer();
            myTimer.Tick += new EventHandler(myTimer_Tick);
            myTimer.Interval = 5000;
            myTimer.Enabled = false;
        }
    
        private Cursor SaveCursor;
        private void button1_Click(object sender, EventArgs e)
        {
            Cursor = Cursors.WaitCursor;
            myTimer.Enabled = true;
        }
    
        void myTimer_Tick(object sender, EventArgs e)
        {
            Cursor = SaveCursor;
            myTimer.Enabled = false;
        }
    }
    
    0 讨论(0)
  • 2020-12-30 04:15

    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;
    
    0 讨论(0)
  • 2020-12-30 04:16

    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.

    0 讨论(0)
提交回复
热议问题