利用多線程句柄設(shè)置鼠標(biāo)忙碌狀態(tài)的實(shí)現(xiàn)方法
當(dāng)我們在讀取數(shù)據(jù)的時(shí)候,或者處理大量數(shù)據(jù)的時(shí)候可能需要把鼠標(biāo)設(shè)置為忙碌狀態(tài),等待返回結(jié)果。下面的代碼可以幫忙實(shí)現(xiàn)這點(diǎn):
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());
}
}
}
這樣在點(diǎn)擊鼠標(biāo)后,鼠標(biāo)會(huì)變成忙碌狀態(tài)一直等待DoSomething這個(gè)方法調(diào)用結(jié)束,然后變回箭頭狀態(tài)。
當(dāng)然你也可以這樣:
// Set the status of the cursor
this.Cursor = Cursor.Busy;
// Do Something
// Set the status of the cursor
this.Cursor = Cursor.Arrow;
如果是在方法里面調(diào)用的話,不能使用this關(guān)鍵字,那你可以這樣做:
private void Method()
{
Curosor.Current = Cursor.WaitCursor;
/// Do Something
Cursor.Current = Cursor.Arrow;
}
相關(guān)文章
C# 操作 access 數(shù)據(jù)庫的實(shí)例代碼
這篇文章主要介紹了C# 操作 access 數(shù)據(jù)庫的實(shí)例代碼,需要的朋友可以參考下2018-03-03C#.Net基于正則表達(dá)式抓取百度百家文章列表的方法示例
這篇文章主要介紹了C#.Net基于正則表達(dá)式抓取百度百家文章列表的方法,結(jié)合實(shí)例形式分析了C#獲取百度百家文章內(nèi)容及使用正則表達(dá)式匹配標(biāo)題、內(nèi)容、地址等相關(guān)操作技巧,需要的朋友可以參考下2017-08-08基于WebRequest.RegisterPrefix的使用詳解
本篇文章對WebRequest.RegisterPrefix的使用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05