Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
519 views
in Technique[技术] by (71.8m points)

c# - Displaying wait cursor in while backgroundworker is running

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;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...