利用多線(xiàn)程句柄設(shè)置鼠標(biāo)忙碌狀態(tài)的實(shí)現(xiàn)方法
當(dāng)我們?cè)谧x取數(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)用的話(huà),不能使用this關(guān)鍵字,那你可以這樣做:
private void Method()
{
Curosor.Current = Cursor.WaitCursor;
/// Do Something
Cursor.Current = Cursor.Arrow;
}
相關(guān)文章
使用linq to xml修改app.config示例(linq讀取xml)
這篇文章主要介紹了使用linq to xml修改app.config示例,需要的朋友可以參考下2014-02-02
WPF利用CommunityToolkit.Mvvm實(shí)現(xiàn)級(jí)聯(lián)選擇器
這篇文章主要介紹了WPF如何利用CommunityToolkit.Mvvm實(shí)現(xiàn)級(jí)聯(lián)選擇器,文中的示例代碼講解詳細(xì),對(duì)我們的學(xué)習(xí)或工作有一定幫助,需要的小伙伴可以參考一下2023-12-12
簡(jiǎn)單聊聊C#字符串構(gòu)建利器StringBuilder
因?yàn)镾tring類(lèi)型代表不可變字符串,所以無(wú)法對(duì)當(dāng)前String類(lèi)型實(shí)例進(jìn)行處理.所以FCL提供了System.Text.StringBuilder類(lèi)型,下面這篇文章主要給大家介紹了關(guān)于C#字符串構(gòu)建利器StringBuilder的相關(guān)資料,需要的朋友可以參考下2022-03-03
C#實(shí)現(xiàn)漢字轉(zhuǎn)區(qū)位碼的示例代碼
區(qū)位碼是一個(gè)4位的十進(jìn)制數(shù),每個(gè)區(qū)位碼都對(duì)應(yīng)著一個(gè)唯一的漢字,區(qū)位碼的前兩位叫做區(qū)碼,后兩位叫做位碼,下面我們就來(lái)看看如何使用C#實(shí)現(xiàn)漢字轉(zhuǎn)區(qū)位碼吧2024-01-01
C#用websocket實(shí)現(xiàn)簡(jiǎn)易聊天功能(服務(wù)端)
這篇文章主要為大家詳細(xì)介紹了C#用websocket實(shí)現(xiàn)簡(jiǎn)易聊天功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
C# 總結(jié)QueueUserWorkItem傳參幾種方式案例詳解
這篇文章主要介紹了C# 總結(jié)QueueUserWorkItem傳參幾種方式案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09
一文詳解C#中重寫(xiě)(override)及覆蓋(new)的區(qū)別
這篇文章主要為大家詳細(xì)介紹了C#中重寫(xiě)(override)及覆蓋(new)這兩個(gè)關(guān)鍵詞的區(qū)別,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-03-03
詳解C#中使用對(duì)象或集合的初始值設(shè)定項(xiàng)初始化的操作
這篇文章主要介紹了詳解C#中使用對(duì)象或集合的初始值設(shè)定項(xiàng)初始化的操作,文中分別講了對(duì)對(duì)象和字典的初始化,需要的朋友可以參考下2016-01-01

