当我们在读取数据的时候,或者处理大量数据的时候可能需要把鼠标设置为忙碌状态,等待返回结果。下面的代码可以帮忙实现这点:
namespace CursorThread
{
public partial class Form1 : Form
{
public delegate int DoSomethingDelegate(int data);
public Form1()
{
InitializeComponent();
}
static int DoSomething(int data)
{
/// <sumary>
/// Do something in this method
/// </sumary>
Thread.Sleep(300);
return data++;
}
private void button1_Click(object sender, EventArgs e)
{
this.Cursor = Cursors.Default;
DoSomethingDelegate d = DoSomething;
IAsyncResult ar = d.BeginInvoke(100,null, null);
while (true)
{
this.Cursor = Cursors.WaitCursor;
if(ar.AsyncWaitHandle.WaitOne(50, false))
{
this.Cursor = Cursors.Arrow;
break;
}
}
//Get the result
int result = d.EndInvoke(ar);
MessageBox.Show(result.ToString());
}
}
}
当然你也可以这样:
// Do Something
// Set the status of the cursor
this.Cursor = Cursor.Arrow;
/// Do Something
Cursor.Current = Cursor.Arrow;
}
新闻热点
疑难解答