手机
当前位置:查字典教程网 >编程开发 >C#教程 >利用多线程句柄设置鼠标忙碌状态的实现方法
利用多线程句柄设置鼠标忙碌状态的实现方法
摘要:当我们在读取数据的时候,或者处理大量数据的时候可能需要把鼠标设置为忙碌状态,等待返回结果。下面的代码可以帮忙实现这点:复制代码代码如下:us...

当我们在读取数据的时候,或者处理大量数据的时候可能需要把鼠标设置为忙碌状态,等待返回结果。下面的代码可以帮忙实现这点:

复制代码 代码如下:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Threading;

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());

}

}

}

这样在点击鼠标后,鼠标会变成忙碌状态一直等待DoSomething这个方法调用结束,然后变回箭头状态。

当然你也可以这样:

复制代码 代码如下:

// Set the status of the cursor

this.Cursor = Cursor.Busy;

// Do Something

// Set the status of the cursor

this.Cursor = Cursor.Arrow;

如果是在方法里面调用的话,不能使用this关键字,那你可以这样做:

复制代码 代码如下:

private void Method()

{

Curosor.Current = Cursor.WaitCursor;

/// Do Something

Cursor.Current = Cursor.Arrow;

}

【利用多线程句柄设置鼠标忙碌状态的实现方法】相关文章:

C# WinForm中Panel实现用鼠标操作滚动条的实例方法

解决C#获取鼠标相对当前窗口坐标的实现方法

解决C#全屏幕截图的实现方法

c# 重载WndProc,实现重写“最小化”的实现方法

解析如何正确使用SqlConnection的实现方法

c#生成缩略图的实现方法

在C#中创建和读取XML文件的实现方法

C#定位txt指定行的方法小例子

C# 语音功能的实现方法

C# 获取打印机当前状态的方法

精品推荐
分类导航